From d1ab08536175fe3eb4bd655446f6e3f9eb4b97c0 Mon Sep 17 00:00:00 2001 From: Scott Worley Date: Mon, 21 Jun 2021 16:00:55 -0700 Subject: [PATCH] Parse command line arguments more extensibly --- git_cache.py | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) 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)) -- 2.44.1