Mercurial > libervia-backend
comparison libervia/cli/arg_tools.py @ 4075:47401850dec6
refactoring: rename `libervia.frontends.jp` to `libervia.cli`
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 02 Jun 2023 14:54:26 +0200 |
parents | libervia/frontends/jp/arg_tools.py@26b7ed2817da |
children | 0d7bb4df2343 |
comparison
equal
deleted
inserted
replaced
4074:26b7ed2817da | 4075:47401850dec6 |
---|---|
1 #!/usr/bin/env python3 | |
2 | |
3 | |
4 # Libervia CLI | |
5 # Copyright (C) 2009-2021 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 libervia.backend.core.i18n import _ | |
21 from libervia.backend.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 '"' + arg.replace('"', '\\"') + '"' | |
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(LiberviaCli): LiberviaCli 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 @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 | |
53 """ | |
54 # FIXME: positional args are not handled correclty | |
55 # if there is more that one, the position is not corrected | |
56 if parser is None: | |
57 parser = host.parser | |
58 | |
59 # we check not optional args to see if there | |
60 # is a corresonding parser | |
61 # else USE args would not work correctly (only for current parser) | |
62 parser_args = [] | |
63 for arg in args: | |
64 if arg.startswith("-"): | |
65 break | |
66 try: | |
67 parser = get_cmd_choices(arg, parser) | |
68 except exceptions.NotFound: | |
69 break | |
70 parser_args.append(arg) | |
71 | |
72 # post_args are remaning given args, | |
73 # without the ones corresponding to parsers | |
74 post_args = args[len(parser_args) :] | |
75 | |
76 opt_args = [] | |
77 pos_args = [] | |
78 actions = {a.dest: a for a in parser._actions} | |
79 for arg, value in use.items(): | |
80 try: | |
81 if arg == "item" and not "item" in actions: | |
82 # small hack when --item is appended to a --items list | |
83 arg = "items" | |
84 action = actions[arg] | |
85 except KeyError: | |
86 if verbose: | |
87 host.disp( | |
88 _( | |
89 "ignoring {name}={value}, not corresponding to any argument (in USE)" | |
90 ).format(name=arg, value=escape(value)) | |
91 ) | |
92 else: | |
93 if verbose: | |
94 host.disp( | |
95 _("arg {name}={value} (in USE)").format( | |
96 name=arg, value=escape(value) | |
97 ) | |
98 ) | |
99 if not action.option_strings: | |
100 pos_args.append(value) | |
101 else: | |
102 opt_args.append(action.option_strings[0]) | |
103 opt_args.append(value) | |
104 return parser_args, opt_args + pos_args + post_args |