From 9d51454b8d68f4eed7adb5f34a61dbb1afc7ef95 Mon Sep 17 00:00:00 2001 From: Sean Morris Date: Thu, 24 Jun 2021 10:39:37 -0700 Subject: [PATCH] Optional command-line keyword arguments The optional command-line arguments(repo-dir and branch_name) are now keyword arguments; this allows us to avoid having to pass an empty branch_name() as the second argument if you want to pass in the repo_dir as the third argument. In the previous configuration, we used positional arguments(gitpuller git_url branch_name repo_dir) now we use keyword arguments: gitpuller git_url --branch_name [BRANCH_NAME] --repo_dir [REPO_DIR] --- nbgitpuller/pull.py | 6 +++--- tests/test_gitpuller.py | 11 ++++++++--- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/nbgitpuller/pull.py b/nbgitpuller/pull.py index ef2aefb7..ddc900a6 100644 --- a/nbgitpuller/pull.py +++ b/nbgitpuller/pull.py @@ -303,14 +303,14 @@ def main(): parser = argparse.ArgumentParser(description='Synchronizes a github repository with a local repository.') parser.add_argument('git_url', help='Url of the repo to sync') - parser.add_argument('branch_name', default=None, help='Branch of repo to sync', nargs='?') - parser.add_argument('repo_dir', default='.', help='Path to clone repo under', nargs='?') + parser.add_argument('--branch_name', default=None, required=False, help='Branch of repo to sync', nargs='?') + parser.add_argument('--repo_dir', default='.', required=False, help='Path to clone repo under', nargs='?') args = parser.parse_args() for line in GitPuller( args.git_url, args.repo_dir, - branch=args.branch_name if args.branch_name else None + branch=args.branch_name ).pull(): print(line) diff --git a/tests/test_gitpuller.py b/tests/test_gitpuller.py index 0055b0da..d7242d97 100644 --- a/tests/test_gitpuller.py +++ b/tests/test_gitpuller.py @@ -99,7 +99,11 @@ def test_initialize(): def command_line_test_helper(remote_path, branch, pusher_path): work_dir = "/".join(os.path.dirname(os.path.abspath(__file__)).split("/")[:-1]) + "/nbgitpuller" try: - cmd = ['python3', 'pull.py', remote_path, branch, pusher_path] + cmd = ['python3', 'pull.py', remote_path] + if branch is not None: + cmd += ['--branch_name', branch] + if pusher_path is not None: + cmd += ['--repo_dir', pusher_path] sp.check_output( cmd, cwd=work_dir @@ -119,8 +123,9 @@ def test_command_line_existing_branch(): assert subprocess_result -def test_command_line_default_branch(): - branch = "" +def test_command_line_no_branch_passed(): + # so it should use the default branch + branch = None with Remote() as remote, Pusher(remote) as pusher: pusher.push_file('README.md', '1') remotepath = "file://%s" % os.path.abspath(remote.path)