Commits

gbrandl  committed a90c5c6

Add -P option to command line mode.

  • Participants
  • Parent commits 6ab39a9
  • Branches trunk

Comments (0)

Files changed (5)

   enhanced. You shouldn't get UnicodeErrors from it anymore if you
   don't give an encoding option.
 
+- Added a ``-P`` option to the command line mode which can be used
+  to give options whose values contain commas or equals signs.
+
+- Added 256-color terminal formatter.
+
 - Added the ``lineanchors`` option to the HTML formatter, thanks to
   Ian Charnas for the idea.
 
 - Gave the line numbers table a CSS class in the HTML formatter.
 
-- Added 256-color terminal formatter.
-
 - Added a Vim 7-like style.
 
 

File docs/pygmentize.1

 .SH SYNOPSIS
 .B \fBpygmentize\fP
 .RI  [-l\ \fI<lexer>\fP]\ [-F\ \fI<filter>\fP[:\fI<options>\fP]]\ [-f\ \fI<formatter>\fP]
-.RI  [-O\ \fI<options>\fP]\ [-o\ \fI<outfile>\fP]\ [\fI<infile>\fP]
+.RI  [-O\ \fI<options>\fP]\ [-P\ \fI<option=value>\fP]\ [-o\ \fI<outfile>\fP]\ [\fI<infile>\fP]
 .br
 .B \fBpygmentize\fP
-.RI -S\ \fI<style>\fP\ -f\ \fI<formatter>\fP\ [-a\ \fI<arg>\fP]\ [-O\ \fI<options>\fP]
+.RI -S\ \fI<style>\fP\ -f\ \fI<formatter>\fP\ [-a\ \fI<arg>\fP]\ [-O\ \fI<options>\fP]\ [-P\ \fI<option=value>\fP]
 .br
 .B \fBpygmentize\fP
 .RI -L\ [\fI<which>\fP\ ...]
 lexers and formatters can be found in the documentation.
 This option can be given multiple times.
 .TP
+.B \-P \fI<option=value>\fP
+This option adds lexer and formatter options like the -O option, but
+you can only give one option per -P. That way, the option value may contain
+commas and equals signs, which it can't with -O.
+.TP
 .B \-S \fI<style>\fP
 Print out style definitions for style \fI<style>\fP and for formatter \fI<formatter>\fP.
 The meaning of the argument given by

File docs/src/cmdline.txt

 expects a list value, separate the list entries with spaces (you'll have to
 quote the option value in this case too, so that the shell doesn't split it).
 
+Since the ``-O`` option argument is split at commas and expects the split values
+to be of the form ``name=value``, you can't give an option value that contains
+commas or equals signs.  Therefore, an option ``-P`` is provided that works like
+``-O`` but can only pass one option per ``-P``. Its value can then contain all
+characters::
+
+    $ pygmentize -P "heading=Pygments, the Python highlighter" ...
+
 Filters are added to the token stream using the ``-F`` option::
 
     $ pygmentize -f html -l pascal -F keywordcase:case=upper main.pas

File pygments/cmdline.py

 
 USAGE = """\
 Usage: %s [-l <lexer>] [-F <filter>[:<options>]] [-f <formatter>]
-          [-O <options>] [-o <outfile>] [<infile>]
+          [-O <options>] [-P <option=value>] [-o <outfile>] [<infile>]
 
-       %s -S <style> -f <formatter> [-a <arg>] [-O <options>]
+       %s -S <style> -f <formatter> [-a <arg>] [-O <options>] [-P <option=value>]
        %s -L [<which> ...]
        %s -H <type> <name>
        %s -h | -V
 With the -O option, you can give the lexer and formatter a comma-
 separated list of options, e.g. ``-O bg=light,python=cool``.
 
+The -P option adds lexer and formatter options like the -O option, but
+you can only give one option per -P. That way, the option value may
+contain commas and equals signs, which it can't with -O, e.g.
+``-P "heading=Pygments, the Python highlighter".
+
 With the -F option, you can add filters to the token stream, you can
 give options in the same way as for -O after a colon (note: there must
 not be spaces around the colon).
 
-The -O and -F options can be given multiple times.
+The -O, -P and -F options can be given multiple times.
 
 With the -S option, print out style definitions for style <style>
 for formatter <formatter>. The argument given by -a is formatter
     usage = USAGE % ((args[0],) * 5)
 
     try:
-        popts, args = getopt.getopt(args[1:], "l:f:F:o:O:LS:a:hVH")
+        popts, args = getopt.getopt(args[1:], "l:f:F:o:O:P:LS:a:hVH")
     except getopt.GetoptError, err:
         print >>sys.stderr, usage
         return 2
     opts = {}
     O_opts = []
+    P_opts = []
     F_opts = []
     for opt, arg in popts:
         if opt == '-O':
             O_opts.append(arg)
+        elif opt == '-P':
+            P_opts.append(arg)
         elif opt == '-F':
             F_opts.append(arg)
         opts[opt] = arg
         return 0
 
     # parse -O options
-    O_opts = _parse_options(O_opts)
+    parsed_opts = _parse_options(O_opts)
     opts.pop('-O', None)
 
+    # parse -P options
+    for p_opt in P_opts:
+        try:
+            name, value = p_opt.split('=', 1)
+        except:
+            parsed_opts[p_opt] = True
+        else:
+            parsed_opts[name] = value
+    opts.pop('-P', None)
+
     # handle ``pygmentize -S``
     S_opt = opts.pop('-S', None)
     a_opt = opts.pop('-a', None)
             return 2
 
         try:
-            O_opts['style'] = S_opt
-            fmter = get_formatter_by_name(f_opt, **O_opts)
+            parsed_opts['style'] = S_opt
+            fmter = get_formatter_by_name(f_opt, **parsed_opts)
         except ClassNotFound, err:
             print >>sys.stderr, err
             return 1
     fmter = opts.pop('-f', None)
     if fmter:
         try:
-            fmter = get_formatter_by_name(fmter, **O_opts)
+            fmter = get_formatter_by_name(fmter, **parsed_opts)
         except (OptionError, ClassNotFound), err:
             print >>sys.stderr, 'Error:', err
             return 1
     if outfn:
         if not fmter:
             try:
-                fmter = get_formatter_for_filename(outfn, **O_opts)
+                fmter = get_formatter_for_filename(outfn, **parsed_opts)
             except (OptionError, ClassNotFound), err:
                 print >>sys.stderr, 'Error:', err
                 return 1
             return 1
     else:
         if not fmter:
-            fmter = TerminalFormatter(**O_opts)
+            fmter = TerminalFormatter(**parsed_opts)
         outfile = sys.stdout
 
     # select lexer
     lexer = opts.pop('-l', None)
     if lexer:
         try:
-            lexer = get_lexer_by_name(lexer, **O_opts)
+            lexer = get_lexer_by_name(lexer, **parsed_opts)
         except (OptionError, ClassNotFound), err:
             print >>sys.stderr, 'Error:', err
             return 1
         infn = args[0]
         if not lexer:
             try:
-                lexer = get_lexer_for_filename(infn, **O_opts)
+                lexer = get_lexer_for_filename(infn, **parsed_opts)
             except (OptionError, ClassNotFound), err:
                 print >>sys.stderr, 'Error:', err
                 return 1
     # No encoding given? Use latin1 if output file given,
     # stdin/stdout encoding otherwise.
     # (This is a compromise, I'm not too happy with it...)
-    if 'encoding' not in O_opts and 'outencoding' not in O_opts:
+    if 'encoding' not in parsed_opts and 'outencoding' not in parsed_opts:
         if outfn:
             # encoding pass-through
             fmter.encoding = 'latin1'

File tests/test_cmdline.py

         self.assert_("<html" in o)
         self.assert_('class="linenos"' in o)
 
+    def test_P_opt(self):
+        filename = os.path.join(testdir, testfile)
+        c, o, e = run_cmdline("-Pfull", "-Ptitle=foo, bar=baz=,", "-fhtml", filename)
+        self.assertEquals(c, 0)
+        self.assert_("<title>foo, bar=baz=,</title>" in o)
+
     def test_F_opt(self):
         filename = os.path.join(testdir, testfile)
         c, o, e = run_cmdline("-Fhighlight:tokentype=Name.Blubb,names=testfile testdir",