00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
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
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
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]
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
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]