Mercurial > libervia-backend
annotate sat_frontends/jp/arg_tools.py @ 3746:fa3dc4ed7906
doc (components): documentation of `comments_max_depth` option and threads conversion:
ActivityPub to XMPP threads conversion is detailed.
fix 364
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 22 Mar 2022 17:00:42 +0100 |
parents | be6d91572633 |
children | 4b842c1fb686 |
rev | line source |
---|---|
3137 | 1 #!/usr/bin/env python3 |
2 | |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 # jp: a SàT command line tool |
3479 | 5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org) |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 # This program is free software: you can redistribute it and/or modify |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 # it under the terms of the GNU Affero General Public License as published by |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 # the Free Software Foundation, either version 3 of the License, or |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 # (at your option) any later version. |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 # This program is distributed in the hope that it will be useful, |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 # GNU Affero General Public License for more details. |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 # You should have received a copy of the GNU Affero General Public License |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 from sat.core.i18n import _ |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 from sat.core import exceptions |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 def escape(arg, smart=True): |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 """format arg with quotes |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
26 |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 @param smart(bool): if True, only escape if needed |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 """ |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
29 if smart and not " " in arg and not '"' in arg: |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 return arg |
3028 | 31 return '"' + arg.replace('"', '\\"') + '"' |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 def get_cmd_choices(cmd=None, parser=None): |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 try: |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 choices = parser._subparsers._group_actions[0].choices |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 return choices[cmd] if cmd is not None else choices |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
38 except (KeyError, AttributeError): |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
39 raise exceptions.NotFound |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 def get_use_args(host, args, use, verbose=False, parser=None): |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 """format args for argparse parser with values prefilled |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 @param host(JP): jp instance |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 @param args(list(str)): arguments to use |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 @param use(dict[str, str]): arguments to fill if found in parser |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 @param verbose(bool): if True a message will be displayed when argument is used or not |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 @param parser(argparse.ArgumentParser): parser to use |
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)
Goffi <goffi@goffi.org>
parents:
2315
diff
changeset
|
50 @return (tuple[list[str],list[str]]): 2 args lists: |
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)
Goffi <goffi@goffi.org>
parents:
2315
diff
changeset
|
51 - parser args, i.e. given args corresponding to parsers |
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)
Goffi <goffi@goffi.org>
parents:
2315
diff
changeset
|
52 - use args, i.e. generated args from use |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 """ |
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)
Goffi <goffi@goffi.org>
parents:
2315
diff
changeset
|
54 # FIXME: positional args are not handled correclty |
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)
Goffi <goffi@goffi.org>
parents:
2315
diff
changeset
|
55 # if there is more that one, the position is not corrected |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 if parser is None: |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 parser = host.parser |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 # we check not optional args to see if there |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 # is a corresonding parser |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 # else USE args would not work correctly (only for current parser) |
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)
Goffi <goffi@goffi.org>
parents:
2315
diff
changeset
|
62 parser_args = [] |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 for arg in args: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
64 if arg.startswith("-"): |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 break |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 try: |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 parser = get_cmd_choices(arg, parser) |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 except exceptions.NotFound: |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 break |
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)
Goffi <goffi@goffi.org>
parents:
2315
diff
changeset
|
70 parser_args.append(arg) |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
71 |
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)
Goffi <goffi@goffi.org>
parents:
2315
diff
changeset
|
72 # post_args are remaning given args, |
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)
Goffi <goffi@goffi.org>
parents:
2315
diff
changeset
|
73 # without the ones corresponding to parsers |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
74 post_args = args[len(parser_args) :] |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
75 |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
76 opt_args = [] |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
77 pos_args = [] |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
78 actions = {a.dest: a for a in parser._actions} |
3028 | 79 for arg, value in use.items(): |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
80 try: |
3028 | 81 if arg == "item" and not "item" in actions: |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
82 # small hack when --item is appended to a --items list |
3028 | 83 arg = "items" |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
84 action = actions[arg] |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
85 except KeyError: |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
86 if verbose: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
87 host.disp( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
88 _( |
3028 | 89 "ignoring {name}={value}, not corresponding to any argument (in USE)" |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
90 ).format(name=arg, value=escape(value)) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
91 ) |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
92 else: |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
93 if verbose: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
94 host.disp( |
3028 | 95 _("arg {name}={value} (in USE)").format( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
96 name=arg, value=escape(value) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
97 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
98 ) |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
99 if not action.option_strings: |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
100 pos_args.append(value) |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
101 else: |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
102 opt_args.append(action.option_strings[0]) |
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
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
103 opt_args.append(value) |
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)
Goffi <goffi@goffi.org>
parents:
2315
diff
changeset
|
104 return parser_args, opt_args + pos_args + post_args |