Last active
August 29, 2015 13:57
-
-
Save mjdorma/9516371 to your computer and use it in GitHub Desktop.
begins: run a series of subcommands
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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