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("--deps", action="store_true", dest="just_deps",
71  help="print file dependencies")
72  parser.add_option("--inorder", "-i", action="store_true", dest="in_order",
73  help="processing in read order (default, can be omitted)")
74 
75  # verbosity options
76  parser.add_option("-q", action="store_const", dest="verbosity", const=0,
77  help="quiet operation, suppressing warnings")
78  parser.add_option("-v", action="count", dest="verbosity",
79  help="increase verbosity")
80  parser.add_option("--verbosity", metavar='level', dest="verbosity", type='int',
81  help=textwrap.dedent("""\
82  set verbosity level
83  0: quiet, suppressing warnings
84  1: default, showing warnings and error locations
85  2: show stack trace
86  3: log property definitions and usage on top level
87  4: log property definitions and usage on all levels"""))
88 
89  # process substitution args
90  try:
91  from rosgraph.names import load_mappings, REMAP
92  mappings = load_mappings(argv)
93  filtered_args = [a for a in argv if REMAP not in a] # filter-out REMAP args
94  except ImportError as e:
95  warning(e)
96  mappings = {}
97  filtered_args = argv
98 
99  parser.set_defaults(just_deps=False, just_includes=False, verbosity=1)
100  (options, pos_args) = parser.parse_args(filtered_args)
101  if options.in_order:
102  message("xacro: in-order processing became default in ROS Melodic. You can drop the option.")
103  options.in_order = True
104 
105  if len(pos_args) != 1:
106  if require_input:
107  parser.error("expected exactly one input file as argument")
108  else:
109  pos_args = [None]
110 
111  options.mappings = mappings
112  return options, pos_args[0]
xacro.color.colorize
def colorize(msg, color, file=sys.stderr, alt_text=None)
Definition: color.py:41
xacro.cli.IndentedHelpFormatterWithNL.__init__
def __init__(self, *args, **kwargs)
Definition: cli.py:55
xacro.color.warning
def warning(*args, **kwargs)
Definition: color.py:62
xacro.color.message
def message(msg, *args, **kwargs)
Definition: color.py:55
xacro.cli.ColoredOptionParser
Definition: cli.py:38
xacro.cli.wrap_with_newlines
def wrap_with_newlines(text, width, **kwargs)
Definition: cli.py:47
xacro.cli.IndentedHelpFormatterWithNL.format_option
def format_option(self, text)
Definition: cli.py:58
xacro.cli.ColoredOptionParser.error
def error(self, message)
Definition: cli.py:39
xacro.cli.IndentedHelpFormatterWithNL
Definition: cli.py:54
xacro.cli._original_wrap
_original_wrap
Definition: cli.py:44
xacro.cli.process_args
def process_args(argv, require_input=True)
Definition: cli.py:65


xacro
Author(s): Stuart Glaser, William Woodall, Robert Haschke
autogenerated on Fri Jan 26 2024 03:50:16