1 # Copyright (C) 2013-2020 Roland Lutz
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software Foundation,
15 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 ## \namespace xorn.command
18 ## Infrastructure for \c xorn command-line scripts.
20 # This module provides some common functionality used by \c xorn
21 # subcommands. It is also where the \c xorn executable provides
22 # information to submodules.
24 # A typical \c xorn subcommand looks like this:
27 # #!/usr/bin/env python2.7
30 # xorn.command.bugreport = 'example@example.com'
34 # options, args = getopt.getopt(
35 # xorn.command.args, '', ['help', 'version'])
36 # except getopt.GetoptError as e:
37 # xorn.command.invalid_arguments(e.msg)
39 # for option, value in options:
40 # # ... process options ...
42 # # ... do something ...
44 # if __name__ == '__main__':
49 from gettext
import gettext
as _
52 ## Email address to which users should send bug reports.
54 # Set this to an appropriate value for your script.
56 bugreport
= xorn
.config
.PACKAGE_BUGREPORT
58 ## Return the value of <tt>argv[0]</tt>.
60 # Python replaces <tt>sys.argv[0]</tt> with an absolute path if the
61 # command was run from the search path. This function tries to
62 # compensate for this by returning just the basename if
63 # <tt>sys.argv[0]</tt> is an absolute path.
66 if sys
.argv
[0].startswith('/'):
67 return os
.path
.basename(sys
.argv
[0])
70 ## Name that was used to invoke the script.
72 # If the script was run by \c xorn, this is <tt>argv[0]</tt> plus a
73 # space character plus the subcommand name. Otherwise, it is the same
74 # as <tt>argv[0]</tt>.
76 # Typically used in the output of \c \--help.
78 program_name
= argv0()
80 ## Basename component of the name that was used to invoke the script.
82 # If the script was run by \c xorn, this is the basename of the
83 # executed script, i.e., \c 'xorn-<i>something</i>'. Otherwise, it is
84 # the same as <tt>argv[0]</tt>, with all text up to and including the
85 # final slash (/), if any, removed.
87 # Typically used in error messages.
89 program_short_name
= os
.path
.basename(argv0())
91 ## List of command arguments.
93 # A list of unparsed arguments, starting with the first argument
94 # following the invocation name. If the script was run by \c xorn,
95 # this is the right-hand part of \c argv with all \c xorn arguments
96 # and the command name stripped. Otherwise, it is the same as
101 ## Print an argument error message to \c sys.stdout and exit.
103 # This command prints an error message of the form
105 # xorn-frobnicate: missing file operand
106 # Try `xorn frobnicate --help' for more information.
108 # to \c sys.stderr and exits with status code \c 1.
110 def invalid_arguments(message
):
111 sys
.stderr
.write(_("%s: %s\n") % (program_short_name
, message
))
112 sys
.stderr
.write(_("Try `%s --help' for more information.\n")
116 ## Print core version.
118 # Prints an output appropriate for the \c \--version option of the core
119 # scripts to \c sys.stdout and exits with status code \c 0.
122 sys
.stdout
.write("%s\n" % xorn
.config
.PACKAGE_STRING
)
123 sys
.stdout
.write(_("Copyright (C) 2020 Roland Lutz\n"))
124 sys
.stdout
.write("\n")
126 "This program is free software; you can redistribute it and/or\n"
127 "modify it under the terms of the GNU General Public License\n"
128 "as published by the Free Software Foundation; either version 2\n"
129 "of the License, or (at your option) any later version.\n"))
130 sys
.stdout
.write("\n")
132 "This program is distributed in the hope that it will be useful,\n"
133 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
134 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
135 "GNU General Public License for more details.\n"))