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


xacro
Author(s): Stuart Glaser, William Woodall, Robert Haschke
autogenerated on Mon Jun 10 2019 15:46:01