Xorn
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
command.py
Go to the documentation of this file.
1 # Copyright (C) 2013-2016 Roland Lutz
2 #
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.
7 #
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.
12 #
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.
16 
17 ## \namespace xorn.command
18 ## Infrastructure for \c xorn command-line scripts.
19 #
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.
23 #
24 # A typical \c xorn subcommand looks like this:
25 #
26 # \code{.py}
27 # #!/usr/bin/python
28 # import xorn.command
29 #
30 # xorn.command.bugreport = 'example@example.com'
31 #
32 # def main():
33 # try:
34 # options, args = getopt.getopt(
35 # xorn.command.args, '', ['help', 'version'])
36 # except getopt.GetoptError as e:
37 # xorn.command.invalid_arguments(e.msg)
38 #
39 # for option, value in options:
40 # # ... process options ...
41 #
42 # # ... do something ...
43 #
44 # if __name__ == '__main__':
45 # main()
46 # \endcode
47 
48 import os.path, sys
49 from gettext import gettext as _
50 import xorn.config
51 
52 ## Email address to which users should send bug reports.
53 #
54 # Set this to an appropriate value for your script.
55 
56 bugreport = xorn.config.PACKAGE_BUGREPORT
57 
58 ## Return the value of <tt>argv[0]</tt>.
59 #
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.
64 
65 def argv0():
66  if sys.argv[0].startswith('/'):
67  return os.path.basename(sys.argv[0])
68  return sys.argv[0]
69 
70 ## Name that was used to invoke the script.
71 #
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>.
75 #
76 # Typically used in the output of \c \--help.
77 
78 program_name = argv0()
79 
80 ## Basename component of the name that was used to invoke the script.
81 #
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.
86 #
87 # Typically used in error messages.
88 
89 program_short_name = os.path.basename(argv0())
90 
91 ## List of command arguments.
92 #
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
97 # <tt>argv[1:]</tt>.
98 
99 args = sys.argv[1:]
100 
101 ## Print an argument error message to \c sys.stdout and exit.
102 #
103 # This command prints an error message of the form
104 # \verbatim
105 # xorn-frobnicate: missing file operand
106 # Try `xorn frobnicate --help' for more information.
107 # \endverbatim
108 # to \c sys.stderr and exits with status code \c 1.
109 
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")
113  % program_name)
114  sys.exit(1)
115 
116 ## Print core version.
117 #
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.
120 
122  sys.stdout.write("%s\n" % xorn.config.PACKAGE_STRING)
123  sys.stdout.write(_("Copyright (C) 2016 Roland Lutz\n"))
124  sys.stdout.write("\n")
125  sys.stdout.write(_(
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")
131  sys.stdout.write(_(
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"))
136  sys.exit(0)
def core_version
Print core version.
Definition: command.py:121
def invalid_arguments
Print an argument error message to sys.stdout and exit.
Definition: command.py:110
def argv0
Return the value of argv[0].
Definition: command.py:65