Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First, it is necessary to make a small distinction. The <code>argparse</code> module does not parse your command-line arguments, the shell does. The shell is responsible for transforming the line you type in the shell into tokens, which are then passed to <code>sys.argv</code>, a python array/sequence of command-line arguments. The <code>argparse</code> module simply makes sense of what appears in <code>sys.argv</code>.</p> <p>This distinction will clarify both of the "errors" you noticed. First, consider <code>-w "-4.5.5-double_non-parallel_gcc"</code> (note the lack of equal sign). The shell parses these two tokens as <code>-w</code> and <code>-4.5.5-double_non-parallel_gcc</code>, and both of these strings are passed to sys.argv. Without the equal sign, this appears to be two options: <code>-w</code> (with no argument) and <code>-4</code> with <code>.5.5-double_non-parallel_gcc</code> as an argument. You need the equals sign so that everything is parsed as a single token.</p> <p><strong>EDITED SECTION</strong></p> <p>As for <code>2&amp;&gt;</code>, it is impossible for argparse to control whether a given token is treated as an argument or not. If something appears in sys.argv, that means your <em>shell</em> is treating it as an argument.</p> <p>The telltale sign here is the error message. Note that the message is not <code>unrecognized arguments: 2&amp;&gt;</code>, but rather, <code>unrecognized arguments: 2</code>. Your shell recognizes "&amp;>" as output redirection, and parses the rest of the line (including the log file) accordingly. The only argument being passed is the "2", because <code>2&amp;&gt;</code> is not a real type of redirection. (<code>&amp;&gt;</code> already covers both stderr and stdout, so what would the <code>2</code> add to it?)</p> <p>In a comment, you claimed that <code>optparse</code> could "handle" the "2&amp;>". That is not actually the case. The <code>optparse</code> module did exactly what <code>argparse</code> does, but <code>optparse</code> does not validate positional arguments like <code>argparse</code> does. In fact, <code>optparse</code> is letting a real programming error (in this case, using <code>2&amp;&gt;</code> as a type of shell redirection) slip by undetected! You should post your original optparse code, but I suspect you parsed your arguments as follows:</p> <pre><code>opt, args = parser.parse_args() </code></pre> <p>Your script takes no positional arguments, so I imagine you did nothing else with <code>args</code> afterwards. But if you were to inspect <code>args</code>, you would find that <code>2</code> was considered a positional argument! </p> <p>In general, if a script takes no positional arguments and you use <code>optparse</code>, it is good practice to verify that you receive no positional arguments, like so:</p> <pre><code>opt, args = parser.parse_args() if args: parser.error("script takes no positional arguments") </code></pre> <p>The <code>argparse</code> module does that work for you, which is what puts it miles ahead of <code>optparse</code> (among other reasons).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload