diff options
author | Kelvin M. Klann <kmk3.code@protonmail.com> | 2022-10-18 17:10:10 -0300 |
---|---|---|
committer | Kelvin M. Klann <kmk3.code@protonmail.com> | 2022-10-22 00:09:39 -0300 |
commit | c648adc9a6e2f50e3b742d2f71a148e9f9e41c89 (patch) | |
tree | d5366a53a087f7631a237bdff3ae7a12fbb1501e /contrib/sort.py | |
parent | sort.py: print usage if there are no arguments (diff) | |
download | firejail-c648adc9a6e2f50e3b742d2f71a148e9f9e41c89.tar.gz firejail-c648adc9a6e2f50e3b742d2f71a148e9f9e41c89.tar.zst firejail-c648adc9a6e2f50e3b742d2f71a148e9f9e41c89.zip |
sort.py: use script name in usage/main docstring
With this, the help section remains consistent regardless of how the
script is called and even if the filename is changed. For example, if
someone renames "sort.py" to "firejail-sort" and puts it somewhere in
`$PATH`.
Example outputs of the script name (using `print(argv[0]); return`):
$ ./contrib/sort.py
./contrib/sort.py
$ python contrib/sort.sh
contrib/sort.py
$ (cd contrib && ./sort.py)
./sort.py
Note: This depends on `os.path` and `sys.argv`, so the imports have to
appear before the docstring. In which case, the docstring has to be
explicitly assigned to `__doc__` (as it ceases to be the first statement
in the file).
Note2: When running `pydoc ./contrib/sort.py`, `argv[0]` becomes
"/usr/bin/pydoc" (using python 3.10.8-1 on Artix Linux).
Diffstat (limited to 'contrib/sort.py')
-rwxr-xr-x | contrib/sort.py | 22 |
1 files changed, 12 insertions, 10 deletions
diff --git a/contrib/sort.py b/contrib/sort.py index 388a29710..638f14516 100755 --- a/contrib/sort.py +++ b/contrib/sort.py | |||
@@ -2,10 +2,16 @@ | |||
2 | # This file is part of Firejail project | 2 | # This file is part of Firejail project |
3 | # Copyright (C) 2014-2022 Firejail Authors | 3 | # Copyright (C) 2014-2022 Firejail Authors |
4 | # License GPL v2 | 4 | # License GPL v2 |
5 | """\ | 5 | |
6 | # Requirements: | ||
7 | # python >= 3.6 | ||
8 | from os import path | ||
9 | from sys import argv, exit as sys_exit, stderr | ||
10 | |||
11 | __doc__ = f"""\ | ||
6 | Sort the arguments of commands in profiles. | 12 | Sort the arguments of commands in profiles. |
7 | 13 | ||
8 | Usage: ./sort.py [/path/to/profile ...] | 14 | Usage: {path.basename(argv[0])} [/path/to/profile ...] |
9 | 15 | ||
10 | The following commands are supported: | 16 | The following commands are supported: |
11 | 17 | ||
@@ -17,10 +23,10 @@ Note that this is only applicable to commands that support multiple arguments. | |||
17 | Keep in mind that this will overwrite your profile(s). | 23 | Keep in mind that this will overwrite your profile(s). |
18 | 24 | ||
19 | Examples: | 25 | Examples: |
20 | $ ./sort.py MyAwesomeProfile.profile | 26 | $ {argv[0]} MyAwesomeProfile.profile |
21 | $ ./sort.py new_profile.profile second_new_profile.profile | 27 | $ {argv[0]} new_profile.profile second_new_profile.profile |
22 | $ ./sort.py ~/.config/firejail/*.{profile,inc,local} | 28 | $ {argv[0]} ~/.config/firejail/*.{{profile,inc,local}} |
23 | $ sudo ./sort.py /etc/firejail/*.{profile,inc,local} | 29 | $ sudo {argv[0]} /etc/firejail/*.{{profile,inc,local}} |
24 | 30 | ||
25 | Exit Codes: | 31 | Exit Codes: |
26 | 0: Success: No profiles needed fixing. | 32 | 0: Success: No profiles needed fixing. |
@@ -29,10 +35,6 @@ Exit Codes: | |||
29 | 101: Info: One or more profiles were fixed. | 35 | 101: Info: One or more profiles were fixed. |
30 | """ | 36 | """ |
31 | 37 | ||
32 | # Requirements: | ||
33 | # python >= 3.6 | ||
34 | from sys import argv, exit as sys_exit, stderr | ||
35 | |||
36 | 38 | ||
37 | def sort_alphabetical(original_items): | 39 | def sort_alphabetical(original_items): |
38 | items = original_items.split(",") | 40 | items = original_items.split(",") |