Closed cbm755 closed 1 year ago
hi!
I agree, this would be a nice improvement.
For the implementation: the Pathlib.Path.name
method should do the job, compare, e.g., "Extract file name from path" on stackoverflow.
If you run things like python3 -c req2flatpak ...
then arg.sysv[0]
will be -c
. https://docs.python.org/3/library/sys.html#sys.argv
I think pathlib.Path.name
is still ok in that case...
We could use __file__
instead of sys.argv
.
Things I'm trying:
a1 = [pathlib.Path(x).name if i == 0 else x for i, x in enumerate(sys.argv)]
a2 = [pathlib.Path(sys.argv[0]).name]
a2.extend(sys.argv[1:])
a3 = [pathlib.Path(__file__).name]
a3.extend(sys.argv[1:])
output_stream.write("# Generated by " + " ".join(argv) + "\n")
I was liked a1
but maybe __file__
is closer in semantics to what we want (?)
Seems __file__
is not always defined? https://stackoverflow.com/questions/9271464/what-does-the-file-variable-mean-do
Quoting from the docs:
__file__
is optional. If set, this attribute’s value must be a string. The import system may opt to leave__file__
unset if it has no semantic meaning (e.g. a module loaded from a database).
For sys.argv:
argv[0]
is the script name (it is operating system dependent whether this is a full pathname or not). If the command was executed using the -c command line option to the interpreter,argv[0]
is set to the string'-c'
. If no script name was passed to the Python interpreter,argv[0]
is the empty string.
More docs on __file__
:
Name of the place from which the module is loaded, e.g. “builtin” for built-in modules and the filename for modules loaded from source. Normally “origin” should be set, but it may be None (the default) which indicates it is unspecified (e.g. for namespace packages).
https://docs.python.org/3/reference/datamodel.html
The pathname of the file from which the module was loaded, if it was loaded from a file. The
__file__
attribute may be missing for certain types of modules, such as C modules that are statically linked into the interpreter. For extension modules loaded dynamically from a shared library, it’s the pathname of the shared library file.
I think you can safely use __file__
. in my experience, __file__
is reliably defined if python scripts are loaded from the filesystem. (it is not defined in scenarios where python scripts are compiled into an executable binary... but that does not matter for what we want to do, because compiling to a binary is not at all our intended runtime scenario).
Alternatively, you could print the arguments as parsed by argparse. But there does not seem to be a built-in way for this, compare e.g. https://stackoverflow.com/q/34992524
I think you can safely use
__file__
. in my experience,__file__
is reliably defined if python scripts are loaded from the filesystem.
This is my experience to and also what a was able to grasp from the docs.
That
/home/cbm/.local/bin/
is correct but not very professional and not really relevant to almost everyone reading the file.Some nice way to get just the exe name from
argv
? or we have to do something likepathlib.Path(argv[...]).name
?I remember also seeing
./
like:which is probably the same fix...