cli.py
Go to the documentation of this file.
1 # Copyright (c) 2015, Open Source Robotics Foundation, Inc.
2 # Copyright (c) 2013, Willow Garage, Inc.
3 # All rights reserved.
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are met:
7 #
8 # * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above copyright
11 # notice, this list of conditions and the following disclaimer in the
12 # documentation and/or other materials provided with the distribution.
13 # * Neither the name of the Open Source Robotics Foundation, Inc.
14 # nor the names of its contributors may be used to endorse or promote
15 # products derived from this software without specific prior
16 # written permission.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 # POSSIBILITY OF SUCH DAMAGE.
29 
30 # Authors: Stuart Glaser, William Woodall, Robert Haschke
31 # Maintainer: Morgan Quigley <morgan@osrfoundation.org>
32 
33 import textwrap
34 from optparse import OptionParser, IndentedHelpFormatter
35 from .color import colorize, warning, message
36 
37 
38 class ColoredOptionParser(OptionParser):
39  def error(self, message):
40  msg = colorize(message, 'red')
41  OptionParser.error(self, msg)
42 
43 
44 _original_wrap = textwrap.wrap
45 
46 
47 def wrap_with_newlines(text, width, **kwargs):
48  result = []
49  for paragraph in text.split('\n'):
50  result.extend(_original_wrap(paragraph, width, **kwargs))
51  return result
52 
53 
54 class IndentedHelpFormatterWithNL(IndentedHelpFormatter):
55  def __init__(self, *args, **kwargs):
56  IndentedHelpFormatter.__init__(self, *args, **kwargs)
57 
58  def format_option(self, text):
59  textwrap.wrap, old = wrap_with_newlines, textwrap.wrap
60  result = IndentedHelpFormatter.format_option(self, text)
61  textwrap.wrap = old
62  return result
63 
64 
65 def process_args(argv, require_input=True):
66  parser = ColoredOptionParser(usage="usage: %prog [options] <input>",
67  formatter=IndentedHelpFormatterWithNL())
68  parser.add_option("-o", dest="output", metavar="FILE",
69  help="write output to FILE instead of stdout")
70  parser.add_option("--inorder", "-i", action="store_true", dest="in_order",
71  help="use processing in read order [default]")
72  parser.add_option("--legacy", action="store_false", dest="in_order",
73  help="use legacy processing order [deprecated]")
74  parser.add_option("--check-order", action="store_true", dest="do_check_order",
75  help="check document for inorder processing", default=False)
76 
77  parser.add_option("--deps", action="store_true", dest="just_deps",
78  help="print file dependencies")
79  parser.add_option("--includes", action="store_true", dest="just_includes",
80  help="only process includes [deprecated]")
81  parser.add_option("--xacro-ns", action="store_false", default=True, dest="xacro_ns",
82  help="require xacro namespace prefix for xacro tags")
83 
84  # verbosity options
85  parser.add_option("-q", action="store_const", dest="verbosity", const=0,
86  help="quiet operation suppressing warnings")
87  parser.add_option("-v", action="count", dest="verbosity",
88  help="increase verbosity")
89  parser.add_option("--verbosity", metavar='level', dest="verbosity", type='int',
90  help=textwrap.dedent("""\
91  set verbosity level
92  0: quiet, suppressing warnings
93  1: default, showing warnings and error locations
94  2: show stack trace
95  3: log property definitions and usage on top level
96  4: log property definitions and usage on all levels"""))
97 
98  # process substitution args
99  try:
100  from rosgraph.names import load_mappings, REMAP
101  mappings = load_mappings(argv)
102  filtered_args = [a for a in argv if REMAP not in a] # filter-out REMAP args
103  except ImportError as e:
104  warning(e)
105  mappings = {}
106  filtered_args = argv
107 
108  parser.set_defaults(just_deps=False, just_includes=False, verbosity=1)
109  (options, pos_args) = parser.parse_args(filtered_args)
110  if options.in_order is None:
111  # --inorder is default, but it's incompatible to --includes
112  options.in_order = not options.just_includes
113  elif options.in_order == True:
114  message("xacro: in-order processing became default in ROS Melodic. You can drop the option.")
115  if options.in_order == False:
116  warning("xacro: Legacy processing is deprecated since ROS Jade and will be removed in N-turtle.")
117  message("To check for compatibility of your document, use option --check-order.", color='yellow')
118  message("For more infos, see http://wiki.ros.org/xacro#Processing_Order", color='yellow')
119 
120  if options.just_includes:
121  warning("xacro: option --includes is deprecated")
122 
123  # --inorder is incompatible to --includes: --inorder processing starts evaluation
124  # while --includes should return the unmodified document
125  if options.in_order and options.just_includes:
126  parser.error("options --inorder and --includes are mutually exclusive")
127 
128  if options.do_check_order:
129  options.in_order = True # check-order implies inorder
130 
131  if len(pos_args) != 1:
132  if require_input:
133  parser.error("expected exactly one input file as argument")
134  else:
135  pos_args = [None]
136 
137  options.mappings = mappings
138  return options, pos_args[0]
def wrap_with_newlines(text, width, kwargs)
Definition: cli.py:47
def format_option(self, text)
Definition: cli.py:58
def message(msg, args, kwargs)
Definition: color.py:55
def warning(args, kwargs)
Definition: color.py:62
def process_args(argv, require_input=True)
Definition: cli.py:65
def __init__(self, args, kwargs)
Definition: cli.py:55
_original_wrap
Definition: cli.py:44
def colorize(msg, color, file=sys.stderr, alt_text=None)
Definition: color.py:41
def error(self, message)
Definition: cli.py:39


xacro
Author(s): Stuart Glaser, William Woodall, Robert Haschke
autogenerated on Thu May 25 2023 02:45:08