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
36 
37 class ColoredOptionParser(OptionParser):
38  def error(self, message):
39  msg = colorize(message, 'red')
40  OptionParser.error(self, msg)
41 
42 
43 _original_wrap = textwrap.wrap
44 def wrap_with_newlines(text, width, **kwargs):
45  result = []
46  for paragraph in text.split('\n'):
47  result.extend(_original_wrap(paragraph, width, **kwargs))
48  return result
49 
50 class IndentedHelpFormatterWithNL(IndentedHelpFormatter):
51  def __init__(self, *args, **kwargs):
52  IndentedHelpFormatter.__init__(self, *args, **kwargs)
53 
54  def format_option(self, text):
55  textwrap.wrap, old = wrap_with_newlines, textwrap.wrap
56  result = IndentedHelpFormatter.format_option(self, text)
57  textwrap.wrap = old
58  return result
59 
60 
61 def process_args(argv, require_input=True):
62  parser = ColoredOptionParser(usage="usage: %prog [options] <input>",
63  formatter=IndentedHelpFormatterWithNL())
64  parser.add_option("-o", dest="output", metavar="FILE",
65  help="write output to FILE instead of stdout")
66  parser.add_option("--oldorder", action="store_false", dest="in_order",
67  help="use traditional processing order [deprecated default]")
68  parser.add_option("--inorder", "-i", action="store_true", dest="in_order",
69  help="use processing in read order")
70  parser.add_option("--check-order", action="store_true", dest="do_check_order",
71  help="check document for inorder processing", default=False)
72 
73  parser.add_option("--deps", action="store_true", dest="just_deps",
74  help="print file dependencies")
75  parser.add_option("--includes", action="store_true", dest="just_includes",
76  help="only process includes")
77  parser.add_option("--xacro-ns", action="store_false", default=True, dest="xacro_ns",
78  help="require xacro namespace prefix for xacro tags")
79 
80  # verbosity options
81  parser.add_option("-q", action="store_const", dest="verbosity", const=0,
82  help="quiet operation suppressing warnings")
83  parser.add_option("-v", action="count", dest="verbosity",
84  help="increase verbosity")
85  parser.add_option("--verbosity", metavar='level', dest="verbosity", type='int',
86  help=textwrap.dedent("""\
87  set verbosity level
88  0: quiet, suppressing warnings
89  1: default, showing warnings and error locations
90  2: show stack trace
91  3: log property definitions and usage on top level
92  4: log property definitions and usage on all levels"""))
93 
94  # process substitution args
95  try:
96  from rosgraph.names import load_mappings, REMAP
97  mappings = load_mappings(argv)
98  filtered_args = [a for a in argv if REMAP not in a] # filter-out REMAP args
99  except ImportError as e:
100  warning(e)
101  mappings = {}
102  filtered_args = argv
103 
104  parser.set_defaults(in_order=False, just_deps=False, just_includes=False,
105  verbosity=1)
106  (options, pos_args) = parser.parse_args(filtered_args)
107 
108  # --inorder is incompatible to --includes: --inorder processing starts evaluation
109  # while --includes should return the unmodified document
110  if options.in_order and options.just_includes:
111  parser.error("options --inorder and --includes are mutually exclusive")
112 
113  if options.do_check_order:
114  options.in_order = True # check-order implies inorder
115 
116  if len(pos_args) != 1:
117  if require_input:
118  parser.error("expected exactly one input file as argument")
119  else:
120  pos_args = [None]
121 
122  options.mappings = mappings
123  return options, pos_args[0]
def wrap_with_newlines(text, width, kwargs)
Definition: cli.py:44
def format_option(self, text)
Definition: cli.py:54
def warning(args, kwargs)
Definition: color.py:28
def process_args(argv, require_input=True)
Definition: cli.py:61
def __init__(self, args, kwargs)
Definition: cli.py:51
_original_wrap
Definition: cli.py:43
def colorize(msg, color, file=sys.stderr, alt_text=None)
Definition: color.py:12
def error(self, message)
Definition: cli.py:38


xacro
Author(s): Stuart Glaser, William Woodall, Robert Haschke
autogenerated on Sat Jun 8 2019 18:04:14