-
Notifications
You must be signed in to change notification settings - Fork 0
/
argsparse -git.py
87 lines (70 loc) · 2.08 KB
/
argsparse -git.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
'''import sys
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("method",type=str,help="Method")
parser.add_argument("--a",type=int,default=5,help="operator A")
parser.add_argument("--b",type=int,default=6,help="operator B")
args=parser.parse_args()
print(args.a*args.b)'''
import os
import argparse
from git.cmd import Git
def cli():
"""
git 命名程序入口
"""
parser = argparse.ArgumentParser(prog='git')
subparsers = parser.add_subparsers(
title='These are common Git commands used in various situations',
metavar='command')
# status
status_parser = subparsers.add_parser(
'status',
help='Show the working tree status')
status_parser.set_defaults(handle=handle_status)
# add
add_parser = subparsers.add_parser(
'add',
help='Add file contents to the index')
add_parser.add_argument(
'pathspec',
help='Files to add content from',
nargs='*')
add_parser.set_defaults(handle=handle_add)
# commit
commit_parser = subparsers.add_parser(
'commit',
help='Record changes to the repository')
commit_parser.add_argument(
'--message', '-m',
help='Use the given <msg> as the commit message',
metavar='msg',
required=True)
commit_parser.set_defaults(handle=handle_commit)
git = Git(os.getcwd())
args = parser.parse_args()
if hasattr(args, 'handle'):
args.handle(git, args)
else:
parser.print_help()
def handle_status(git, args):
"""
处理 status 命令
"""
cmd = ['git', 'status']
output = git.execute(cmd)
print(output)
def handle_add(git, args):
"""
处理 add 命令
"""
cmd = ['git', 'add']
output = git.execute(cmd)
print(output)
def handle_commit(git, args):
"""
处理 -m <msg> 命令
"""
cmd = ['git', 'commit', '-m', args.message]
output = git.execute(cmd)
print(output)