cli.py
Go to the documentation of this file.
00001 # Copyright (c) 2015, Open Source Robotics Foundation, Inc.
00002 # Copyright (c) 2013, Willow Garage, Inc.
00003 # All rights reserved.
00004 #
00005 # Redistribution and use in source and binary forms, with or without
00006 # modification, are permitted provided that the following conditions are met:
00007 #
00008 #     * Redistributions of source code must retain the above copyright
00009 #       notice, this list of conditions and the following disclaimer.
00010 #     * Redistributions in binary form must reproduce the above copyright
00011 #       notice, this list of conditions and the following disclaimer in the
00012 #       documentation and/or other materials provided with the distribution.
00013 #     * Neither the name of the Open Source Robotics Foundation, Inc.
00014 #       nor the names of its contributors may be used to endorse or promote
00015 #       products derived from this software without specific prior
00016 #       written permission.
00017 #
00018 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00019 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00022 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00023 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00024 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00025 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00026 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00027 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00028 # POSSIBILITY OF SUCH DAMAGE.
00029 
00030 # Authors: Stuart Glaser, William Woodall, Robert Haschke
00031 # Maintainer: Morgan Quigley <morgan@osrfoundation.org>
00032 
00033 import textwrap
00034 from optparse import OptionParser, IndentedHelpFormatter
00035 from rosgraph.names import load_mappings, REMAP
00036 from .color import colorize
00037 
00038 class ColoredOptionParser(OptionParser):
00039     def error(self, message):
00040         msg = colorize(message, 'red')
00041         OptionParser.error(self, msg)
00042 
00043 
00044 _original_wrap = textwrap.wrap
00045 def wrap_with_newlines(text, width, **kwargs):
00046     result = []
00047     for paragraph in text.split('\n'):
00048         result.extend(_original_wrap(paragraph, width, **kwargs))
00049     return result
00050 
00051 class IndentedHelpFormatterWithNL(IndentedHelpFormatter):
00052     def __init__(self, *args, **kwargs):
00053         IndentedHelpFormatter.__init__(self, *args, **kwargs)
00054 
00055     def format_option(self, text):
00056         textwrap.wrap, old = wrap_with_newlines, textwrap.wrap
00057         result = IndentedHelpFormatter.format_option(self, text)
00058         textwrap.wrap = old
00059         return result
00060 
00061 
00062 def process_args(argv, require_input=True):
00063     parser = ColoredOptionParser(usage="usage: %prog [options] <input>",
00064                                  formatter=IndentedHelpFormatterWithNL())
00065     parser.add_option("-o", dest="output", metavar="FILE",
00066                       help="write output to FILE instead of stdout")
00067     parser.add_option("--oldorder", action="store_false", dest="in_order",
00068                       help="use traditional processing order [deprecated default]")
00069     parser.add_option("--inorder", "-i", action="store_true", dest="in_order",
00070                       help="use processing in read order")
00071     parser.add_option("--check-order", action="store_true", dest="do_check_order",
00072                       help="check document for inorder processing", default=False)
00073 
00074     parser.add_option("--deps", action="store_true", dest="just_deps",
00075                       help="print file dependencies")
00076     parser.add_option("--includes", action="store_true", dest="just_includes",
00077                       help="only process includes")
00078     parser.add_option("--xacro-ns", action="store_false", default=True, dest="xacro_ns",
00079                       help="require xacro namespace prefix for xacro tags")
00080 
00081     # verbosity options
00082     parser.add_option("-q", action="store_const", dest="verbosity", const=0,
00083                       help="quiet operation suppressing warnings")
00084     parser.add_option("-v", action="count", dest="verbosity",
00085                       help="increase verbosity")
00086     parser.add_option("--verbosity", metavar='level', dest="verbosity", type='int',
00087                       help=textwrap.dedent("""\
00088                       set verbosity level
00089                       0: quiet, suppressing warnings
00090                       1: default, showing warnings and error locations
00091                       2: show stack trace
00092                       3: log property definitions and usage on top level
00093                       4: log property definitions and usage on all levels"""))
00094 
00095     # process substitution args
00096     mappings = load_mappings(argv)
00097 
00098     parser.set_defaults(in_order=False, just_deps=False, just_includes=False,
00099                         verbosity=1)
00100     filtered_args = [a for a in argv if REMAP not in a]  # filter-out REMAP args
00101     (options, pos_args) = parser.parse_args(filtered_args)
00102 
00103     if options.in_order and options.just_includes:
00104         parser.error("options --inorder and --includes are mutually exclusive")
00105 
00106     if options.do_check_order:
00107         options.in_order = True  # check-order implies inorder
00108 
00109     if len(pos_args) != 1:
00110         if require_input:
00111             parser.error("expected exactly one input file as argument")
00112         else:
00113             pos_args = [None]
00114 
00115     options.mappings = mappings
00116     return options, pos_args[0]


xacro
Author(s): Stuart Glaser, William Woodall, Robert Haschke
autogenerated on Fri Jun 24 2016 04:05:50