From c648adc9a6e2f50e3b742d2f71a148e9f9e41c89 Mon Sep 17 00:00:00 2001 From: "Kelvin M. Klann" Date: Tue, 18 Oct 2022 17:10:10 -0300 Subject: 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). --- contrib/sort.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) (limited to 'contrib') 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 @@ # 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: @@ -17,10 +23,10 @@ Note that this is only applicable to commands that support multiple arguments. 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. @@ -29,10 +35,6 @@ Exit Codes: 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(",") -- cgit v1.2.3-54-g00ecf