Skip to content

Instantly share code, notes, and snippets.

@mjdorma
Last active August 29, 2015 13:57
Show Gist options
  • Save mjdorma/9516371 to your computer and use it in GitHub Desktop.
Save mjdorma/9516371 to your computer and use it in GitHub Desktop.
begins: run a series of subcommands
"""
Run a series of subcommands from the script file.
"""
import shlex
import begin
import __main__
@begin.subcommand
@begin.convert(script=begin.utils.tofile())
def script(script):
"Execute subcommands from the input script."
# Find the main.
for attr in dir(__main__):
main = getattr(__main__, attr)
if isinstance(main, begin.main.Program):
break
else:
raise Exception("Failed to find begin's start")
# Run each subcommand.
for line in script.readlines():
line = line.strip()
if not line or line.startswith("#"):
continue
command = shlex.split(line)
opts = main._parser.parse_args(command)
begin.cmdline.apply_options(main.__wrapped__, opts,
run_main=False, sub_group=main._group,
collector=main._collector)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment