Groups¶
Argument groups¶
It has been implement the simple argument group as described as argparse.ArgumentParser.add_argument_group()
.
The parameters of the command_tree.groups.ArgumentGroup
are the exact same like the argparse one.
Usage:
groups/arg_group.py¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | from command_tree import CommandTree, ArgumentGroup
tree = CommandTree()
@tree.root()
class Root(object):
grp1 = ArgumentGroup(tree, "platypus")
@tree.leaf()
@grp1.argument("--foo")
@grp1.argument("--bar")
def add(self, foo = 42, bar = 21):
return foo + bar
print(tree.execute())
|
Result:
$ python groups/arg_group.py add -h
usage: arg_group.py add [-h] [--foo FOO] [--bar BAR]
optional arguments:
-h, --help show this help message and exit
platypus:
--foo FOO
--bar BAR
Mutual exclusion¶
It has been implement the mutually exclusive argument group as described as argparse.ArgumentParser.add_mutually_exclusive_group()
.
The parameters of the command_tree.groups.MutuallyExclusiveGroup
are the exact same like the argparse one.
Usage:
groups/mutex.py¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | from command_tree import CommandTree, MutuallyExclusiveGroup
tree = CommandTree()
@tree.root()
class Root(object):
grp1 = MutuallyExclusiveGroup(tree, required = True)
@tree.leaf()
@grp1.argument("--foo")
@grp1.argument("--bar")
def add(self, foo = 42, bar = 21):
return foo + bar
print(tree.execute())
|
Result:
$ python groups/mutex.py add --foo 1 --bar 2
usage: mutex.py add [-h] (--foo FOO | --bar BAR)
mutex.py add: error: argument --bar: not allowed with argument --foo
Mutex group in argument group¶
If you want to add a mutex group into an argument group, it’s possible:
groups/mutex_in_arg.py¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | from command_tree import CommandTree, ArgumentGroup, MutuallyExclusiveGroup
tree = CommandTree()
@tree.root()
class Root(object):
arg_grp = ArgumentGroup(tree, "platypus")
mutex = MutuallyExclusiveGroup(tree, required = True, argument_group = arg_grp)
@tree.leaf()
@mutex.argument("--foo")
@mutex.argument("--bar")
def add(self, foo = 42, bar = 21):
return foo + bar
print(tree.execute())
|