def test_success_argparser(self): parser = ArgumentParser(description="my tool description") parser.add_argument('myarg1', action="store", help="my help 1", type=list) parser.add_argument('myarg2', action="store", help="my help 2", type=int) parser.add_argument("--myarg3", "-m", action="store", help="my help 3") subparser = parser.add_subparsers(help="the choices you will make", dest="mysubparser") sb1 = subparser.add_parser("option1", help="the first value") sb1.add_argument("suboption1", help="the first sub option option") sb1.add_argument("suboption2", help="the first sub option option", choices=['hello', 'goodbye'], default="hello") sb1 = subparser.add_parser("option2", help="the second value") sb1.add_argument("suboption1", help="the first sub option option") sb1.add_argument("suboption3", help="the second sub option option") sb1.add_argument("--suboptionflag1", "-s", help="the bool opt flag") sb1.add_argument("--suboptionflag2", "-d", action="store_true", help="the second sub option flag") sb1.add_argument("--suboptlistflag", "-l", nargs="+", help="listy flag") creatorObj = bc.CreateDescriptor(parser, execname='/path/to/myscript.py', verbose=True, tags={ "purpose": "testing-creator", "foo": "bar" }) fil = './test-created-argparse-descriptor.json' creatorObj.save(fil) invof = './test-created-argparse-inputs.json' args = parser.parse_args([['val1', 'val2'], '2', 'option2', 'subval1', 'subval3', '--suboptionflag1', 't1', '--suboptionflag2']) invo = creatorObj.createInvocation(args) with open(invof, 'w') as fhandle: fhandle.write(json.dumps(invo, indent=4)) self.assertIsNone(bosh(['validate', fil])) self.assertIsNone(bosh(['invocation', fil, '-i', invof]))
def makeBoutiques(parser, arguments): import boutiques.creator as bc import os.path as op import json desc = bc.CreateDescriptor(parser, execname=op.basename(__file__)) basename = op.splitext(__file__)[0] desc.save(basename + ".json") invo = desc.createInvocation(arguments) with open(basename + "_inputs.json", "w") as fhandle: fhandle.write(json.dumps(invo, indent=4))
def test_print_warning(self): parser = ArgumentParser(description="my tool description") parser.add_argument("--myarg", "-m", action="store", help="my help", dest="==SUPPRESS==") creatorObj = bc.CreateDescriptor(parser, execname='/path/to/myscript.py', verbose=True, tags={ "purpose": "testing-creator", "foo": "bar" }) out, err = self.capfd.readouterr() self.assertIn("[ WARNING ]", out)
def make_descriptor(parser, arguments=None): import boutiques.creator as bc basename = "dipy_deterministic_tracking.py" desc = bc.CreateDescriptor(parser, execname=op.basename(basename), tags={"domain": ["neuroinformatics", "image processing", "mri", "noise"]}) desc.save(basename + ".json") if arguments is not None: invo = desc.createInvocation(arguments) invo.pop("boutiques") with open(basename + "_inputs.json", "w") as fhandle: fhandle.write(json.dumps(invo, indent=4))
def test_success_argparser(self): parser = ArgumentParser(description="my tool description") parser.add_argument('myarg1', action="store", help="my help 1", type=list) parser.add_argument('myarg2', action="store", help="my help 2", type=int) parser.add_argument("--myarg3", "-m", action="store", help="my help 3") subparser = parser.add_subparsers(help="the choices you will make") sb1 = subparser.add_parser("option1", help="the first value") sb1.add_argument("suboption1", help="the first sub option option") sb1.add_argument("suboption2", help="the first sub option option", choices=['hello', 'goodbye'], default="hello") sb1 = subparser.add_parser("option2", help="the second value") sb1.add_argument("suboption1", help="the first sub option option") sb1.add_argument("suboption3", help="the second sub option option") sb1.add_argument("--suboptionflag1", "-s", help="the bool opt flag") sb1.add_argument("--suboptionflag2", "-d", action="store_true", help="the second sub option flag") creatorObj = bc.CreateDescriptor(parser, execname='/path/to/myscript.py', verbose=True, tags={ "purpose": "testing-creator", "foo": "bar" }) fil = './test-created-argparse-descriptor.json' creatorObj.save(fil) assert bosh(['validate', fil]) is None
action='store_true', help='Whether ' 'or not to break degrees into hemispheres or not', default=False) parser.add_argument('--log', action='store_true', help='Determines ' 'axis scale for plotting.', default=False) parser.add_argument('--debug', action='store_true', help='flag to store ' 'temp files along the path of processing.', default=False) parser.add_argument("--stc", action="store", help='A file for slice ' 'timing correction. Options are a TR sequence file ' '(where each line is the shift in TRs), ' 'up (ie, bottom to top), down (ie, top to bottom), ' 'and interleaved.', default=None) return parser if __name__ == "__main__": myparser = makeParser() newDescriptor = bc.CreateDescriptor(myparser, execname="ndmg_bids") newDescriptor.save("ndmg_new.json")
def gen_boutique_descriptors(help_dir, outdir='afni_boutiques', verbose=True): """ Generates ``boutiques`` descriptors for each AFNI command in `help_dir` Parameters ---------- help_dir : str Path to directory with AFNI help files outdir : str, optional Path to directory where generated descriptors should be saved. Default: 'afni_boutiques' verbose : bool, optional Whether to print status messages of which commands are being parsed. Default: True Returns ------- descriptors : list List of paths to generated boutiques descriptor JSON files """ outdir = _gen_outdir(outdir) help_dir = Path(help_dir).resolve() descriptors = [] for tool in help_dir.glob('*.complete.bash'): tool_name = tool.name.replace('.complete.bash', '') if verbose: print('Generating JSON for {}.'.format(tool_name)) help_fname = get_help_file(help_dir, tool_name) out_fname = outdir.joinpath('{}.json'.format(tool_name)) # get hypothetical parameters (both flag and positional!) params = parse_help(help_fname, putative=_get_parsed_args(tool))[0] params += [{'param': f} for f in _get_usage_params(help_fname)] # construct an empty parser to hold all parameters parser = argparse.ArgumentParser(add_help=False) for param in params: # ensure argument not already in parser (and not invalid / empty) arg = param.get('param') previous = [f.option_strings for f in parser._actions] + [['']] # ensure argument not already in parser (and not invalid / empty) if arg not in list(chain.from_iterable(previous)): # get info about parameter, if we have it kwargs = {'required': True} if arg == '-input' else {} kwargs.update({'dest': '{}'.format(arg.strip('-').upper( ))} if arg.startswith('-') else {}) desc = param.get('help', 'NA') if desc != 'NA': desc = ' '.join([f.strip() for f in desc.split('\n')]) # add parameter to parser parser.add_argument(arg, type=str, help=desc, **kwargs) # save out new boutiques descriptor bout = bc.CreateDescriptor(parser, execname=tool_name) bout.save(out_fname) descriptors.append(out_fname.as_posix()) return descriptors