Closed kfuku52 closed 1 year ago
yes/no options can be stored as bool using strtobool. Please check csubst for example.
I introduced strtobool for --tissue_detect as an example. Please check and apply it for other options. https://github.com/kfuku52/amalgkit/commit/f85b762ebec67934fa9d698e5a20393a86d3c97e
Just to add to this, I think we can simplify usage as well for arguments that use 'yes|no'. For example amalgkit getfastq --redo yes
can be reduced to just amalgkit getfastq --redo
, while amalgkit getfastq --redo no
would just be amalgkit getfastq
without the option.
I'm not positive about that. Here's how I often end up...
my_progrem -i infile --hard-to-remember-flag
my_progrem -i infile
my_progrem -i infile --another-flag
my_progrem -i infile --another-flag --hard-to-remember-flag
If it's like --hard-to-remember-flag yes
, we don't have to open a webpage or print a very long help message every time we tweak the parameters.
So I don't 100% get how strtobool interacts with argparse. I assumed it would convert argument input yes
to True
and input no
to False
. So a flag --flag1
would be False
if the default
was no
and there wasn't any additional input.
Assuming --flag1 no
, in the code I could then ask:
if args.flag1:
print("true!")
else:
print("false!")
instead of
if args.flag1 == 'yes':
...
correct?
I switched the getfastq download flags to strtobool like this:
pge.add_argument('--aws', metavar='yes|no', default='no', type=strtobool, required=False, action='store',
help='default=%(default)s: Download SRA files from Amazon Cloud (AWS), if available.')
But when getting
This is actually not true, they were stored as 1 or 0.args.aws
inside getfastq
, they are still stored as 'yes' and 'no`, rather than 'true' or 'false'.
Scratch that, the problem was somewhere else!
I went through all of amalgkit to convert everything to strtobool
where there are yes|no
options.
I also introduced parent parsers to eliminate repeated code.
I couldn't figure out a way to do this with a single parent parser holding all the shared options. For example:
--out_dir
is by every subcommand.
--metadata
is used by most subcommands.
But putting both --out_dir
and --metadata
in the same parent parser, both options would show up for example for amalgkit metadata -h
, which does have --out_dir
but does not have --metadata
.
So I put those options into different parent parsers parent_parser_out
and parent_parser_meta
. These can then be combined in any way for the specific subparsers, like so:
# Sub parser: integrate
pin = subparsers.add_parser('integrate', help='see `amalgkit proc -h`', parents=[parent_parser_out, parent_parser_meta, parent_parser_threads])
[...] subparser specific arguments
# Sub parser: sanity
psa = subparsers.add_parser('sanity', help='see `amalgkit sanity -h`', parents=[parent_parser_out, parent_parser_meta])
[...] subparser specific arguments
# Sub parser: curate
pcu = subparsers.add_parser('curate', help='see `amalgkit curate -h`', parents=[parent_parser_out, parent_parser_meta, parent_parser_batch])
[...] subparser specific arguments
Sounds good!
Pushed the update here:
https://github.com/kfuku52/amalgkit/commit/23a02c8c8c0f51fbd56a77d60b7577d6110d71e2
closing this for now, then.
The arguments shared among subparsers can be simplified like this. We can specify
parents=
in aadd_parser
. https://stackoverflow.com/questions/7498595/python-argparse-add-argument-to-multiple-subparsers