Basic example¶
argparse¶
basic-argparse.py¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | from argparse import ArgumentParser
parser = ArgumentParser()
subparsers1 = parser.add_subparsers(dest = 'subcommand')
command1parser = subparsers1.add_parser('command1')
command1parser.add_argument("arg1")
def command1_handler(args):
return int(args.arg1) / 2
command1parser.set_defaults(func = command1_handler)
command2parser = subparsers1.add_parser('command2')
command2parser.add_argument("arg1")
def command2_handler(args):
return int(args.arg1) * 2
command2parser.set_defaults(func = command2_handler)
args = parser.parse_args()
print(args.func(args))
|
command-tree¶
basic.py¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | from command_tree import CommandTree
tree = CommandTree()
@tree.root()
class Root(object):
@tree.leaf()
@tree.argument()
def command1(self, arg1):
return int(arg1) / 2
@tree.leaf()
@tree.argument()
def command2(self, arg1):
return int(arg1) * 2
print(tree.execute())
|