Merge pull request #46 from Ede123/options

Facilitate command line usage
This commit is contained in:
Tobias Oberstein 2016-03-22 16:20:41 +01:00
commit 0d6725e05d

View file

@ -3120,99 +3120,110 @@ class HeaderedFormatter(optparse.IndentedHelpFormatter):
# GZ: would prefer this to be in a function or class scope, but tests etc need # GZ: would prefer this to be in a function or class scope, but tests etc need
# access to the defaults anyway # access to the defaults anyway
_options_parser = optparse.OptionParser( _options_parser = optparse.OptionParser(
usage="%prog [-i input.svg] [-o output.svg] [OPTIONS]", usage="%prog [INPUT.SVG [OUTPUT.SVG]] [OPTIONS]",
description=("If the input/output files are specified with a svgz" description=("If the input/output files are not specified, stdin/stdout are used. "
" extension, then compressed SVG is assumed. If the input file is not" "If the input/output files are specified with a svgz extension, "
" specified, stdin is used. If the output file is not specified, " "then compressed SVG is assumed."),
" stdout is used."), formatter=HeaderedFormatter(max_help_position=33),
formatter=HeaderedFormatter(max_help_position=30),
version=VER) version=VER)
_options_parser.add_option("--disable-simplify-colors",
action="store_false", dest="simple_colors", default=True,
help="won't convert all colors to #RRGGBB format")
_options_parser.add_option("--disable-style-to-xml",
action="store_false", dest="style_to_xml", default=True,
help="won't convert styles into XML attributes")
_options_parser.add_option("--disable-group-collapsing",
action="store_false", dest="group_collapse", default=True,
help="won't collapse <g> elements")
_options_parser.add_option("--create-groups",
action="store_true", dest="group_create", default=False,
help="create <g> elements for runs of elements with identical attributes")
_options_parser.add_option("--enable-id-stripping",
action="store_true", dest="strip_ids", default=False,
help="remove all un-referenced ID attributes")
_options_parser.add_option("--enable-comment-stripping",
action="store_true", dest="strip_comments", default=False,
help="remove all <!-- --> comments")
_options_parser.add_option("--shorten-ids",
action="store_true", dest="shorten_ids", default=False,
help="shorten all ID attributes to the least number of letters possible")
_options_parser.add_option("--shorten-ids-prefix",
action="store", type="string", dest="shorten_ids_prefix", default="",
help="shorten all ID attributes with a custom prefix")
_options_parser.add_option("--disable-embed-rasters",
action="store_false", dest="embed_rasters", default=True,
help="won't embed rasters as base64-encoded data")
_options_parser.add_option("--keep-unreferenced-defs",
action="store_true", dest="keep_defs", default=False,
help="won't remove elements within the defs container that are unreferenced")
_options_parser.add_option("--keep-editor-data",
action="store_true", dest="keep_editor_data", default=False,
help="won't remove Inkscape, Sodipodi, Adobe Illustrator or Sketch elements and attributes")
_options_parser.add_option("--remove-metadata",
action="store_true", dest="remove_metadata", default=False,
help="remove <metadata> elements (which may contain license metadata etc.)")
_options_parser.add_option("--renderer-workaround",
action="store_true", dest="renderer_workaround", default=True,
help="work around various renderer bugs (currently only librsvg) (default)")
_options_parser.add_option("--no-renderer-workaround",
action="store_false", dest="renderer_workaround", default=True,
help="do not work around various renderer bugs (currently only librsvg)")
_options_parser.add_option("--strip-xml-prolog",
action="store_true", dest="strip_xml_prolog", default=False,
help="won't output the <?xml ?> prolog")
_options_parser.add_option("--enable-viewboxing",
action="store_true", dest="enable_viewboxing", default=False,
help="changes document width/height to 100%/100% and creates viewbox coordinates")
# GZ: this is confusing, most people will be thinking in terms of
# decimal places, which is not what decimal precision is doing
_options_parser.add_option("-p", "--set-precision",
action="store", type=int, dest="digits", default=5,
help="set number of significant digits (default: %default)")
_options_parser.add_option("-i",
action="store", dest="infilename", help=optparse.SUPPRESS_HELP)
_options_parser.add_option("-o",
action="store", dest="outfilename", help=optparse.SUPPRESS_HELP)
_options_parser.add_option("-q", "--quiet", _options_parser.add_option("-q", "--quiet",
action="store_true", dest="quiet", default=False, action="store_true", dest="quiet", default=False,
help="suppress non-error output") help="suppress non-error output")
_options_parser.add_option("-v", "--verbose", _options_parser.add_option("-v", "--verbose",
action="store_true", dest="verbose", default=False, action="store_true", dest="verbose", default=False,
help="verbose log output (eg report processed elements and such)") help="verbose output (optimization statistics, etc.)")
_options_parser.add_option("--indent", _options_parser.add_option("-i",
action="store", type="string", dest="indent_type", default="space", action="store", dest="infilename", metavar="INPUT.SVG",
help="alternative way to specify input filename")
_options_parser.add_option("-o",
action="store", dest="outfilename", metavar="OUTPUT.SVG",
help="alternative way to specify output filename")
_option_group_optimization = optparse.OptionGroup(_options_parser, "Optimization")
_option_group_optimization.add_option("-p", "--set-precision",
action="store", type=int, dest="digits", default=5, metavar="NUM",
help="set number of significant digits (default: %default)")
_option_group_optimization.add_option("--disable-simplify-colors",
action="store_false", dest="simple_colors", default=True,
help="won't convert all colors to #RRGGBB format")
_option_group_optimization.add_option("--disable-style-to-xml",
action="store_false", dest="style_to_xml", default=True,
help="won't convert styles into XML attributes")
_option_group_optimization.add_option("--disable-group-collapsing",
action="store_false", dest="group_collapse", default=True,
help="won't collapse <g> elements")
_option_group_optimization.add_option("--create-groups",
action="store_true", dest="group_create", default=False,
help="create <g> elements for runs of elements with identical attributes")
_option_group_optimization.add_option("--keep-editor-data",
action="store_true", dest="keep_editor_data", default=False,
help="won't remove Inkscape, Sodipodi, Adobe Illustrator or Sketch elements and attributes")
_option_group_optimization.add_option("--keep-unreferenced-defs",
action="store_true", dest="keep_defs", default=False,
help="won't remove elements within the defs container that are unreferenced")
_option_group_optimization.add_option("--renderer-workaround",
action="store_true", dest="renderer_workaround", default=True,
help="work around various renderer bugs (currently only librsvg) (default)")
_option_group_optimization.add_option("--no-renderer-workaround",
action="store_false", dest="renderer_workaround", default=True,
help="do not work around various renderer bugs (currently only librsvg)")
_options_parser.add_option_group(_option_group_optimization)
_option_group_document = optparse.OptionGroup(_options_parser, "SVG document")
_option_group_document.add_option("--strip-xml-prolog",
action="store_true", dest="strip_xml_prolog", default=False,
help="won't output the XML prolog (<?xml ?>)")
_option_group_document.add_option("--remove-metadata",
action="store_true", dest="remove_metadata", default=False,
help="remove <metadata> elements (which may contain license/author information etc.)")
_option_group_document.add_option("--enable-comment-stripping",
action="store_true", dest="strip_comments", default=False,
help="remove all comments (<!-- -->)")
_option_group_document.add_option("--disable-embed-rasters",
action="store_false", dest="embed_rasters", default=True,
help="won't embed rasters as base64-encoded data")
_option_group_document.add_option("--enable-viewboxing",
action="store_true", dest="enable_viewboxing", default=False,
help="changes document width/height to 100%/100% and creates viewbox coordinates")
_options_parser.add_option_group(_option_group_document)
_option_group_formatting = optparse.OptionGroup(_options_parser, "Output formatting")
_option_group_formatting.add_option("--indent",
action="store", type="string", dest="indent_type", default="space", metavar="TYPE",
help="indentation of the output: none, space, tab (default: %default)") help="indentation of the output: none, space, tab (default: %default)")
_options_parser.add_option("--nindent", _option_group_formatting.add_option("--nindent",
action="store", type=int, dest="indent_depth", default=1, action="store", type=int, dest="indent_depth", default=1, metavar="NUM",
help="depth of the indentation, i.e. number of spaces/tabs: (default: %default)") help="depth of the indentation, i.e. number of spaces/tabs: (default: %default)")
_options_parser.add_option("--no-line-breaks", _option_group_formatting.add_option("--no-line-breaks",
action="store_false", dest="newlines", default=True, action="store_false", dest="newlines", default=True,
help="do not create line breaks in output (also disables indentation; might be overriden by xml:space=\"preserve\")") help="do not create line breaks in output"
_options_parser.add_option("--strip-xml-space", "(also disables indentation; might be overriden by xml:space=\"preserve\")")
_option_group_formatting.add_option("--strip-xml-space",
action="store_true", dest="strip_xml_space_attribute", default=False, action="store_true", dest="strip_xml_space_attribute", default=False,
help="strip the xml:space=\"preserve\" attribute from the root SVG element") help="strip the xml:space=\"preserve\" attribute from the root SVG element")
_options_parser.add_option("--protect-ids-noninkscape", _options_parser.add_option_group(_option_group_formatting)
_option_group_ids = optparse.OptionGroup(_options_parser, "ID attributes")
_option_group_ids.add_option("--enable-id-stripping",
action="store_true", dest="strip_ids", default=False,
help="remove all unreferenced IDs")
_option_group_ids.add_option("--shorten-ids",
action="store_true", dest="shorten_ids", default=False,
help="shorten all IDs to the least number of letters possible")
_option_group_ids.add_option("--shorten-ids-prefix",
action="store", type="string", dest="shorten_ids_prefix", metavar="PREFIX",
help="add custom prefix to shortened IDs")
_option_group_ids.add_option("--protect-ids-noninkscape",
action="store_true", dest="protect_ids_noninkscape", default=False, action="store_true", dest="protect_ids_noninkscape", default=False,
help="Don't change IDs not ending with a digit") help="don't remove IDs not ending with a digit")
_options_parser.add_option("--protect-ids-list", _option_group_ids.add_option("--protect-ids-list",
action="store", type="string", dest="protect_ids_list", default=None, action="store", type="string", dest="protect_ids_list", metavar="LIST",
help="Don't change IDs given in a comma-separated list") help="don't remove IDs given in this comma-separated list")
_options_parser.add_option("--protect-ids-prefix", _option_group_ids.add_option("--protect-ids-prefix",
action="store", type="string", dest="protect_ids_prefix", default=None, action="store", type="string", dest="protect_ids_prefix", metavar="PREFIX",
help="Don't change IDs starting with the given prefix") help="don't remove IDs starting with the given prefix")
_options_parser.add_option_group(_option_group_ids)
@ -3227,7 +3238,12 @@ def maybe_gziped_file(filename, mode="r"):
def parse_args(args=None, ignore_additional_args=False): def parse_args(args=None, ignore_additional_args=False):
options, rargs = _options_parser.parse_args(args) options, rargs = _options_parser.parse_args(args)
if rargs and not ignore_additional_args: if rargs:
if not options.infilename:
options.infilename = rargs.pop(0)
if not options.outfilename and rargs:
options.outfilename = rargs.pop(0)
if not ignore_additional_args and rargs:
_options_parser.error("Additional arguments not handled: %r, see --help" % rargs) _options_parser.error("Additional arguments not handled: %r, see --help" % rargs)
if options.digits < 0: if options.digits < 0:
_options_parser.error("Can't have negative significant digits, see --help") _options_parser.error("Can't have negative significant digits, see --help")