Compare commits
2 Commits
41554ea286
...
227d6ccd30
| Author | SHA1 | Date | |
|---|---|---|---|
| 227d6ccd30 | |||
| 25febeecb1 |
7
how_to/Change_02.md
Normal file
7
how_to/Change_02.md
Normal file
@@ -0,0 +1,7 @@
|
||||
- cli: Add argument parser
|
||||
|
||||
The real Git executable has multiple sub-commands, like 'git init', 'git commit',
|
||||
etc. Let's use Python's built-in argument parser argparse to implement sub-commands.
|
||||
|
||||
You can see on the other side which changes were made. Now we can run 'ugit init'
|
||||
and see "Hello, World!" printed out.
|
||||
20
ugit/cli.py
20
ugit/cli.py
@@ -1,2 +1,22 @@
|
||||
import argparse
|
||||
|
||||
|
||||
def main():
|
||||
args = parse_args()
|
||||
args.func(args)
|
||||
|
||||
|
||||
def parse_args():
|
||||
parser = argparse.ArgumentParser()
|
||||
|
||||
commands = parser.add_subparsers(dest="command")
|
||||
commands.required = True
|
||||
|
||||
init_parser = commands.add_parser("init")
|
||||
init_parser.set_defaults(func=init)
|
||||
|
||||
return parser.parse_args()
|
||||
|
||||
|
||||
def init(args):
|
||||
print("Hello, World!")
|
||||
|
||||
Reference in New Issue
Block a user