comparison frontends/src/jp/arg_tools.py @ 2315:8d9bd5d77336

jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
author Goffi <goffi@goffi.org>
date Sat, 08 Jul 2017 21:45:14 +0200
parents
children f4e05600577b
comparison
equal deleted inserted replaced
2314:01f0a954d506 2315:8d9bd5d77336
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3
4 # jp: a SàT command line tool
5 # Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 from sat.core.i18n import _
21 from sat.core import exceptions
22
23
24 def escape(arg, smart=True):
25 """format arg with quotes
26
27 @param smart(bool): if True, only escape if needed
28 """
29 if smart and not ' ' in arg and not '"' in arg:
30 return arg
31 return u'"' + arg.replace(u'"',u'\\"') + u'"'
32
33
34 def get_cmd_choices(cmd=None, parser=None):
35 try:
36 choices = parser._subparsers._group_actions[0].choices
37 return choices[cmd] if cmd is not None else choices
38 except (KeyError, AttributeError):
39 raise exceptions.NotFound
40
41
42 def get_use_args(host, args, use, verbose=False, parser=None):
43 """format args for argparse parser with values prefilled
44
45 @param host(JP): jp instance
46 @param args(list(str)): arguments to use
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
49 @param parser(argparse.ArgumentParser): parser to use
50 """
51 if parser is None:
52 parser = host.parser
53
54 # we check not optional args to see if there
55 # is a corresonding parser
56 # else USE args would not work correctly (only for current parser)
57 cmd_args = []
58 for arg in args:
59 if arg.startswith('-'):
60 break
61 try:
62 parser = get_cmd_choices(arg, parser)
63 except exceptions.NotFound:
64 break
65 cmd_args.append(arg)
66
67 # we remove command args
68 # they'll be in returned list (use_args)
69 del args[:len(cmd_args)]
70
71 opt_args = []
72 pos_args = []
73 actions = {a.dest: a for a in parser._actions}
74 for arg, value in use.iteritems():
75 try:
76 if arg == u'item' and not u'item' in actions:
77 # small hack when --item is appended to a --items list
78 arg = u'items'
79 action = actions[arg]
80 except KeyError:
81 if verbose:
82 host.disp(_(u'ignoring {name}={value}, not corresponding to any argument (in USE)').format(
83 name=arg,
84 value=escape(value)))
85 else:
86 if verbose:
87 host.disp(_(u'arg {name}={value} set (in USE)').format(name=arg, value=escape(value)))
88 if not action.option_strings:
89 pos_args.append(value)
90 else:
91 opt_args.append(action.option_strings[0])
92 opt_args.append(value)
93 return cmd_args + opt_args + pos_args