Change in osmo-dev[master]: use argparse

osmith gerrit-no-reply at lists.osmocom.org
Thu Nov 8 10:32:20 UTC 2018


osmith has uploaded this change for review. ( https://gerrit.osmocom.org/11672


Change subject: use argparse
......................................................................

use argparse

Change-Id: Ib2207346003ffb28432b1cd1bac6250ad8a1a060
---
M src/gits
1 file changed, 40 insertions(+), 48 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/osmo-dev refs/changes/72/11672/1

diff --git a/src/gits b/src/gits
index 3528915..316fd03 100755
--- a/src/gits
+++ b/src/gits
@@ -26,8 +26,6 @@
 doc = '''gits: conveniently manage several git subdirectories.
 Instead of doing the 'cd foo; git status; cd ../bar; git status' dance, this
 helps to save your time with: status, fetch, rebase, ...
-
-See 'gits help'
 '''
 
 re_status_mods = re.compile('^\t(modified|deleted):.*')
@@ -223,21 +221,12 @@
     print(format_summaries(infos))
 
 
-def cmd_status():
-    if len(sys.argv) > 2:
-        error('no arguments allowed')
-    print_status()
-
-
-def cmd_do(argv=None):
-    if argv is None:
-        argv = sys.argv[2:]
+def cmd_do(argv):
     for git_dir in git_dirs():
         git(git_dir, *argv, may_fail=True, section_marker=True)
 
 
-def cmd_sh():
-    cmd = sys.argv[2:]
+def cmd_sh(cmd):
     for git_dir in git_dirs():
         print('\n===== %s =====' % git_dir)
         print('+ %s' % cmd_to_str(cmd))
@@ -358,10 +347,6 @@
     return orig_branch
 
 
-def cmd_fetch():
-    cmd_do(['fetch'] + sys.argv[2:])
-
-
 def cmd_rebase():
     skipped = []
     for git_dir in git_dirs():
@@ -385,39 +370,46 @@
         print('\nskipped: %s' % ' '.join(skipped))
 
 
-commands = {
-    'status': (cmd_status, 'Show a branch summary and indicate modifications.'),
-    'fetch': (cmd_fetch, "Run 'git fetch' in each clone; useful before `gits rebase`."),
-    'rebase': (cmd_rebase, 'Interactively ff-merge master and rebase current branches.'),
-    'sh': (cmd_sh, 'Run arbitrary shell command in each clone (`gits sh echo hi`).'),
-    'do': (cmd_do, 'Run arbitrary git command in each clone (`gits do clean -dxf`).'),
-    'help': (cmd_help, 'List gits commands.'),
-}
+def parse_args():
+    parser = argparse.ArgumentParser(description=doc)
+    sub = parser.add_subparsers(title="action", dest="action")
+    sub.required = True
 
-aliases = {
-    's': 'status',
-    'st': 'status',
-    'r': 'rebase',
-    're': 'rebase',
-    'f': 'fetch',
-    '-h': 'help',
-}
+    sub.add_parser("status", aliases=["st", "s"],
+        help="show a branch summary and indicate modifications")
+
+    fetch = sub.add_parser("fetch", aliases=["f"],
+                   help="run 'git fetch' in each clone; useful before `gits rebase`")
+    fetch.add_argument("remainder", nargs=argparse.REMAINDER,
+               help="additional arguments to be passed to git fetch")
+
+    sub.add_parser("rebase", aliases=["r", "re"],
+        help="interactively ff-merge master and rebase current branches")
+
+    sh = sub.add_parser("sh",
+        help="run arbitrary shell command in each clone (`gits sh echo hi`)")
+    sh.add_argument("remainder", nargs=argparse.REMAINDER,
+        help="command to run in each clone")
+
+    do = sub.add_parser("do",
+        help="run arbitrary git command in each clone (`gits do clean -dxf`)")
+    do.add_argument("remainder", nargs=argparse.REMAINDER,
+        help="git command to run in each clone")
+
+    return parser.parse_args()
+
 
 if __name__ == '__main__':
-
-    if len(sys.argv) < 2:
-        usage('Pass at least one argument to tell me what to do.')
-
-    command_str = sys.argv[1]
-    alias_for = aliases.get(command_str)
-    if alias_for:
-        command_str = alias_for
-    command = commands.get(command_str)
-
-    if command:
-        func, doc = command
-        func()
-    else:
-        error('unknown command: %r\n`gits help` may help' % command_str)
+    args = parse_args()
+    if args.action in ["status", "s", "st"]:
+        print_status()
+    elif args.action in ["fetch", "f"]:
+        cmd_do(['fetch'] + args.remainder)
+    elif args.action in ["rebase", "r"]:
+        cmd_rebase()
+    elif args.action == "sh":
+        cmd_sh(args.remainder)
+    elif args.action == "do":
+        cmd_do(args.remainder)
 
 # vim: shiftwidth=4 expandtab tabstop=4

-- 
To view, visit https://gerrit.osmocom.org/11672
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: osmo-dev
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib2207346003ffb28432b1cd1bac6250ad8a1a060
Gerrit-Change-Number: 11672
Gerrit-PatchSet: 1
Gerrit-Owner: osmith <osmith at sysmocom.de>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20181108/77827f10/attachment.html>


More information about the gerrit-log mailing list