[Python-ideas] making a module callable

Ben Finney ben+python at benfinney.id.au
Sun Nov 24 09:07:13 CET 2013


Steven D'Aprano <steve at pearwood.info> writes:

> I'm serious, by the way. It's a nice Unix trick to have a single
> executable do different things depending on what name it is called by.
> Inspecting argv[0] lets you do that.

Even if the executable only does one thing, it's still good to be able
to *rename* the program and not have to change the usage and error
messages::

    import os
    import sys

    progname = os.path.basename(__file__)

    # …
    sys.stdout.write(
            "{progname}: Couldn't frobnicate the spangule.\n".format(
                progname=progname))

So, definitely ‘sys.argv’ needs to continue having all command-line
arguments, including the command name used to invoke the program.

-- 
 \       “We must respect the other fellow's religion, but only in the |
  `\       sense and to the extent that we respect his theory that his |
_o__)     wife is beautiful and his children smart.” —Henry L. Mencken |
Ben Finney



More information about the Python-ideas mailing list