Skip to content

Commit

Permalink
sort.py: use script name in usage/main docstring
Browse files Browse the repository at this point in the history
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".
  • Loading branch information
kmk3 committed Oct 19, 2022
1 parent 63b2857 commit 58e403b
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions contrib/sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@
# This file is part of Firejail project
# Copyright (C) 2014-2022 Firejail Authors
# License GPL v2
"""\

# Requirements:
# python >= 3.6
from os import path
from sys import argv, exit as sys_exit, stderr

__doc__ = f"""\
Sort the arguments of commands in profiles.
Usage: ./sort.py [/path/to/profile ...]
Usage: {path.basename(argv[0])} [/path/to/profile ...]
The following commands are supported:
Expand All @@ -17,21 +23,17 @@
Keep in mind that this will overwrite your profile(s).
Examples:
$ ./sort.py MyAwesomeProfile.profile
$ ./sort.py new_profile.profile second_new_profile.profile
$ ./sort.py ~/.config/firejail/*.{profile,inc,local}
$ sudo ./sort.py /etc/firejail/*.{profile,inc,local}
$ {argv[0]} MyAwesomeProfile.profile
$ {argv[0]} new_profile.profile second_new_profile.profile
$ {argv[0]} ~/.config/firejail/*.{{profile,inc,local}}
$ sudo {argv[0]} /etc/firejail/*.{{profile,inc,local}}
Exit Codes:
0: Success: No profiles needed fixing.
1: Error: One or more profiles could not be processed correctly.
101: Info: One or more profiles were fixed.
"""

# Requirements:
# python >= 3.6
from sys import argv, exit as sys_exit, stderr


def sort_alphabetical(original_items):
items = original_items.split(",")
Expand Down

0 comments on commit 58e403b

Please sign in to comment.