From: Scott Worley Date: Mon, 21 Jun 2021 23:00:55 +0000 (-0700) Subject: Parse command line arguments more extensibly X-Git-Tag: v1.4.0~2 X-Git-Url: http://git.scottworley.com/git-cache/commitdiff_plain/d1ab08536175fe3eb4bd655446f6e3f9eb4b97c0?hp=35000f728e66533de9b0cf6e8b040f9cdb82c1a7 Parse command line arguments more extensibly --- diff --git a/git_cache.py b/git_cache.py index 1363f11..d43df26 100644 --- a/git_cache.py +++ b/git_cache.py @@ -4,6 +4,7 @@ # implementation details for my comfort. So we re-implement here half of # nix's builtins.fetchGit. :( +import argparse import functools import hashlib import logging @@ -155,13 +156,29 @@ def ensure_rev_available(repo: Repo, ref: Ref, rev: Rev) -> Path: def _main() -> None: - if len(sys.argv) == 3: - print('{1} {0}'.format(*fetch(Repo(sys.argv[1]), Ref(sys.argv[2])))) - elif len(sys.argv) == 4: - print(ensure_rev_available( - Repo(sys.argv[1]), Ref(sys.argv[2]), Rev(sys.argv[3]))) + parser = argparse.ArgumentParser( + description='Cache remote git repositories locally.', + epilog='example usage: git-cache https://github.com/NixOS/nixpkgs.git master') + parser.add_argument( + 'repo', + metavar='Repo', + type=Repo, + help='Git repository URL') + parser.add_argument( + 'ref', + metavar='Ref', + type=Ref, + help='Ref (branch or tag) in the git repo') + parser.add_argument( + 'rev', + metavar='Rev', + type=Rev, + nargs='?', + help='Ensure that this revision is present. ' + + 'If this revision is already present locally, no network operations are performed.') + args = parser.parse_args() + + if args.rev is None: + print('{1} {0}'.format(*fetch(args.repo, args.ref))) else: - usage = '''usage: git-cache repo ref [rev] -example: git-cache https://github.com/NixOS/nixpkgs.git master''' - print(usage, file=sys.stderr) - sys.exit(1) + print(ensure_rev_available(args.repo, args.ref, args.rev))