Skip to content

Commit c648adc

Browse files
committed
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).
1 parent b1bf9a8 commit c648adc

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

contrib/sort.py

+12-10
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@
22
# This file is part of Firejail project
33
# Copyright (C) 2014-2022 Firejail Authors
44
# 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"""\
612
Sort the arguments of commands in profiles.
713
8-
Usage: ./sort.py [/path/to/profile ...]
14+
Usage: {path.basename(argv[0])} [/path/to/profile ...]
915
1016
The following commands are supported:
1117
@@ -17,10 +23,10 @@
1723
Keep in mind that this will overwrite your profile(s).
1824
1925
Examples:
20-
$ ./sort.py MyAwesomeProfile.profile
21-
$ ./sort.py new_profile.profile second_new_profile.profile
22-
$ ./sort.py ~/.config/firejail/*.{profile,inc,local}
23-
$ sudo ./sort.py /etc/firejail/*.{profile,inc,local}
26+
$ {argv[0]} MyAwesomeProfile.profile
27+
$ {argv[0]} new_profile.profile second_new_profile.profile
28+
$ {argv[0]} ~/.config/firejail/*.{{profile,inc,local}}
29+
$ sudo {argv[0]} /etc/firejail/*.{{profile,inc,local}}
2430
2531
Exit Codes:
2632
0: Success: No profiles needed fixing.
@@ -29,10 +35,6 @@
2935
101: Info: One or more profiles were fixed.
3036
"""
3137

32-
# Requirements:
33-
# python >= 3.6
34-
from sys import argv, exit as sys_exit, stderr
35-
3638

3739
def sort_alphabetical(original_items):
3840
items = original_items.split(",")

0 commit comments

Comments
 (0)