Note that there are some explanatory texts on larger screens.

plurals
  1. POPython: switching from optparse to argparse
    text
    copied!<p>After switching from optparse to argparse - I'm experiencing strange errors. Argparse parse args only if leave no space:</p> <pre><code>myScript.py -oOpt </code></pre> <p>or put an equal sign:</p> <pre><code>myScript.py -o=Opt </code></pre> <p>and it doesn't work the normal way:</p> <pre><code>myScript.py -o Opt </code></pre> <p>Here's my argparse initialization:</p> <pre><code>#!/usr/bin/env python # to get description use the -h flag import argparse, os, sys # ====================== # Python2.7 is expected: if sys.version_info[0] != 2 or sys.version_info[1] &lt; 7: sys.exit('This program needs Python2.7+') # ========== # preambule: desc = """Enter dirs in the current dir and makes gro out of state.cpt there.""" # parser = argparse.ArgumentParser() parser = argparse.ArgumentParser(description=desc, version='2.3', formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument('-w', '--workWith', help = 'to specify a Gromacs exec suffix', dest = 'wW', action = 'store', default = '-4.5.5-single', ) parser.add_argument('-g', '--gro', help = '.gro postfix: &lt;nameOfTheDir&gt;&lt;postfix&gt;.gro', dest = 'myGroPostfix', action = 'store', default = "_membrane", ) parser.add_argument('-H', '--here', help = 'toggles - single (current) dir behaviour (the output will be state.gro)', dest = 'Here', action = 'store_true', ) parser.add_argument('-D', '--dirs', help = 'include these dirs (python\'s rgxp in SINGLE quotes), defaults to \'\'', dest = 'inclDirs', action = 'store', default = '', ) args = parser.parse_args() print args.wW </code></pre> <p><strong>Edit</strong>:</p> <p>Even more:</p> <pre><code> gmx_bk-simulate-mems.py -j bk-runs-mpi.bash -p 1 -w="-4.5.5-double_non-parallel_gcc" 2&amp;&gt; ../`date +%Y-%b-%d-%H%M%S`.log &amp; </code></pre> <p>gives:</p> <pre><code> gmx_bk-simulate-mems.py: error: unrecognized arguments: 2 </code></pre> <p>it looks like <code>argparse</code> treats <code>2&amp;&gt;</code> as option (or <code>2&amp;&gt;</code> and <code>../date +%Y-%b-%d-%H%M%S.log</code> as options)!</p> <p><strong>Edit 2</strong>:</p> <p>So to summarize: </p> <ul> <li><p>For <code>argparse</code> - <code>"-4.5.5-double_non-parallel_gcc"</code> is a bad option name - and that's why it is required to write is as <code>-w="-4.5.5-double_non-parallel_gcc"</code>. For <code>optparse</code> and <code>bash</code> (!) this is fine. <code>bash</code> even gives an error at <code>-w="-4.5.5-double_non-parallel_gcc"</code> - it thinks that the arg is <code>="-4.5.5-double_non-parallel_gcc"</code> (!);</p></li> <li><p>There's no such thing as <code>2&amp;&gt;</code>. <code>2&gt;</code> should might be used and it gives no errors;</p></li> <li><p>This is <code>shell</code> who splits the line into args, not <code>python</code>;</p></li> <li><p><code>argparse</code> is much better than <code>optparse</code>.</p></li> </ul>
 

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