comparison frontends/src/jp/arg_tools.py @ 2317:f4e05600577b

jp (arg_tools): args is not modified anymore in get_use_args + fixed args returned + parser_args are returned separatly (return is now a tuple)
author Goffi <goffi@goffi.org>
date Sat, 08 Jul 2017 22:49:55 +0200
parents 8d9bd5d77336
children 5129a0506739
comparison
equal deleted inserted replaced
2316:7b448ac50a69 2317:f4e05600577b
45 @param host(JP): jp instance 45 @param host(JP): jp instance
46 @param args(list(str)): arguments to use 46 @param args(list(str)): arguments to use
47 @param use(dict[str, str]): arguments to fill if found in parser 47 @param use(dict[str, str]): arguments to fill if found in parser
48 @param verbose(bool): if True a message will be displayed when argument is used or not 48 @param verbose(bool): if True a message will be displayed when argument is used or not
49 @param parser(argparse.ArgumentParser): parser to use 49 @param parser(argparse.ArgumentParser): parser to use
50 @return (tuple[list[str],list[str]]): 2 args lists:
51 - parser args, i.e. given args corresponding to parsers
52 - use args, i.e. generated args from use
50 """ 53 """
54 # FIXME: positional args are not handled correclty
55 # if there is more that one, the position is not corrected
51 if parser is None: 56 if parser is None:
52 parser = host.parser 57 parser = host.parser
53 58
54 # we check not optional args to see if there 59 # we check not optional args to see if there
55 # is a corresonding parser 60 # is a corresonding parser
56 # else USE args would not work correctly (only for current parser) 61 # else USE args would not work correctly (only for current parser)
57 cmd_args = [] 62 parser_args = []
58 for arg in args: 63 for arg in args:
59 if arg.startswith('-'): 64 if arg.startswith('-'):
60 break 65 break
61 try: 66 try:
62 parser = get_cmd_choices(arg, parser) 67 parser = get_cmd_choices(arg, parser)
63 except exceptions.NotFound: 68 except exceptions.NotFound:
64 break 69 break
65 cmd_args.append(arg) 70 parser_args.append(arg)
66 71
67 # we remove command args 72 # post_args are remaning given args,
68 # they'll be in returned list (use_args) 73 # without the ones corresponding to parsers
69 del args[:len(cmd_args)] 74 post_args = args[len(parser_args):]
70 75
71 opt_args = [] 76 opt_args = []
72 pos_args = [] 77 pos_args = []
73 actions = {a.dest: a for a in parser._actions} 78 actions = {a.dest: a for a in parser._actions}
74 for arg, value in use.iteritems(): 79 for arg, value in use.iteritems():
88 if not action.option_strings: 93 if not action.option_strings:
89 pos_args.append(value) 94 pos_args.append(value)
90 else: 95 else:
91 opt_args.append(action.option_strings[0]) 96 opt_args.append(action.option_strings[0])
92 opt_args.append(value) 97 opt_args.append(value)
93 return cmd_args + opt_args + pos_args 98 return parser_args, opt_args + pos_args + post_args