Skip to content

Commit b2012a5

Browse files
committed
type=bool is not the way to do boolean options. added add_boolean_option and used it
1 parent 31433ff commit b2012a5

File tree

1 file changed

+56
-19
lines changed

1 file changed

+56
-19
lines changed

pythonforandroid/toolchain.py

Lines changed: 56 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,31 @@ def shprint(command, *args, **kwargs):
175175
# exit(1)
176176

177177

178+
def add_boolean_option(parser, names, no_names=None,
179+
default=True, dest=None, description=None):
180+
group = parser.add_argument_group(description=description)
181+
if not isinstance(names, (list,tuple)):
182+
names = [names]
183+
if dest is None:
184+
dest = names[0].strip("-").replace("-","_")
185+
def add_dashes(x):
186+
return x if x.startswith("-") else "--"+x
187+
opts = [add_dashes(x) for x in names]
188+
group.add_argument(
189+
*opts, help=("(this is the default)" if default else None),
190+
dest=dest, action='store_true')
191+
if no_names is None:
192+
def add_no(x):
193+
x = x.lstrip("-")
194+
return ("no_"+x) if "_" in x else ("no-"+x)
195+
no_names = [add_no(x) for x in names]
196+
opts = [add_dashes(x) for x in no_names]
197+
group.add_argument(
198+
*opts, help=(None if default else "(this is the default)"),
199+
dest=dest, action='store_false')
200+
parser.set_defaults(**{dest:default})
201+
202+
178203
def require_prebuilt_dist(func):
179204
'''Decorator for ToolchainCL methods. If present, the method will
180205
automatically make sure a dist has been built before continuing
@@ -2513,23 +2538,32 @@ def __init__(self):
25132538
'--requirements',
25142539
help='Dependencies of your app, should be recipe names or Python modules',
25152540
default='')
2541+
2542+
add_boolean_option(
2543+
parser, ["allow-download", "allow_download"],
2544+
default=False,
2545+
description='Whether to allow binary dist download:')
2546+
2547+
add_boolean_option(
2548+
parser, ["allow-build", "allow_build"],
2549+
default=True,
2550+
description='Whether to allow compilation of a new distribution:')
2551+
2552+
add_boolean_option(
2553+
parser, ["force-build", "force_build"],
2554+
default=False,
2555+
description='Whether to force compilation of a new distribution:')
2556+
25162557
parser.add_argument(
2517-
'--allow_download', help='Allow binary dist download.',
2518-
default=False, type=bool)
2519-
parser.add_argument(
2520-
'--allow_build', help='Allow compilation of a new distribution.',
2521-
default=True, type=bool)
2522-
parser.add_argument(
2523-
'--force_build', help='Force compilation of a new distribution.',
2524-
default=False, type=bool)
2525-
parser.add_argument(
2526-
'--extra_dist_dirs', help='Directories in which to look for distributions',
2527-
default='')
2528-
parser.add_argument(
2529-
'--require_perfect_match', help=('Whether the dist recipes must '
2530-
'perfectly match those requested.'),
2531-
type=bool, default=False)
2558+
'--extra-dist-dirs', '--extra_dist_dirs',
2559+
dest='extra_dist_dirs', default='',
2560+
help='Directories in which to look for distributions')
25322561

2562+
add_boolean_option(
2563+
parser, ["require-perfect-match", "require_perfect_match"],
2564+
default=False,
2565+
description=('Whether the dist recipes must perfectly match '
2566+
'those requested'))
25332567

25342568
self._read_configuration()
25352569

@@ -2601,11 +2635,14 @@ def recipes(self, args):
26012635
parser = argparse.ArgumentParser(
26022636
description="List all the available recipes")
26032637
parser.add_argument(
2604-
"--compact", action="store_true",
2638+
"--compact", action="store_true", default=False,
26052639
help="Produce a compact list suitable for scripting")
2606-
parser.add_argument(
2607-
'--color', type=bool, default=True,
2608-
help='Whether the output should be coloured')
2640+
2641+
add_boolean_option(
2642+
parser, ["color"],
2643+
default=True,
2644+
description='Whether the output should be colored:')
2645+
26092646
args = parser.parse_args(args)
26102647

26112648
if args.compact:

0 commit comments

Comments
 (0)