Skip to content

Commit

Permalink
Add support for single command-line supporting both "release" and "as…
Browse files Browse the repository at this point in the history
…set"

* Install new executable named "githubrelease" supporting both
  "release" and "asset" commands.
* Support for directly use of "github_release.py"
* Multi-parser usage was adapted from https://chase-seibert.github.io/blog/2014/03/21/python-multilevel-argparse.html

Examples:

  python github_release.py release j0057/iplbapi list
  githubrelease release j0057/iplbapi list
  githubrelease asset j0057/iplbapi download
  • Loading branch information
jcfr committed Feb 2, 2017
1 parent 4d4f655 commit d4b30c0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
42 changes: 36 additions & 6 deletions github_release.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import glob
import json
import os
import sys
import tempfile


Expand Down Expand Up @@ -296,8 +297,8 @@ def with_error_handling(*args, **kwargs):
return with_error_handling


def _gh_parser(commands):
parser = argparse.ArgumentParser(description=__doc__)
def _gh_parser(commands, prog=None):
parser = argparse.ArgumentParser(description=__doc__, prog=prog)
parser.add_argument("repo_name", type=str)
subparsers = parser.add_subparsers(help='sub-command help')

Expand All @@ -320,8 +321,8 @@ def _gh_parser(commands):


@handle_http_error
def gh_release():
args = _gh_parser(RELEASE_COMMANDS).parse_args()
def gh_release(argv=None, prog=None):
args = _gh_parser(RELEASE_COMMANDS, prog).parse_args(argv)
func = args.func
return func(*[vars(args).get(arg_name, None) for arg_name in func.description["params"]])

Expand All @@ -337,7 +338,36 @@ def gh_release():


@handle_http_error
def gh_asset():
args = _gh_parser(ASSET_COMMANDS).parse_args()
def gh_asset(argv=None, prog=None):
args = _gh_parser(ASSET_COMMANDS, prog).parse_args(argv)
func = args.func
return func(*[vars(args).get(arg_name, None) for arg_name in func.description["params"]])


def main():
prog = os.path.basename(sys.argv[0])
parser = argparse.ArgumentParser(
description=__doc__,
usage="""%s [-h] {release, asset} ...
positional arguments:
{release, asset}
sub-command help
release Manage releases (list, create, delete, ...)
asset Manage release assets (upload, download, ...)
optional arguments:
-h, --help show this help message and exit
""" % prog)
parser.add_argument('command', help='Subcommand to run')
args = parser.parse_args(sys.argv[1:2])
if "gh_%s" % args.command not in globals():
print("Unrecognized command")
parser.print_help()
exit(1)
globals()["gh_%s" % args.command](
sys.argv[2:], "%s %s" % (prog, args.command))


if __name__ == '__main__':
main()
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
install_requires=requirements,
entry_points={
'console_scripts': [
'githubrelease = github_release:main',
'github-release = github_release:gh_release',
'github-asset = github_release:gh_asset'
]})

0 comments on commit d4b30c0

Please sign in to comment.