<div class="gmail_extra"><div class="gmail_quote">2012/4/26 Walter Valenti <span dir="ltr"><<a href="mailto:waltervalenti@yahoo.it" target="_blank">waltervalenti@yahoo.it</a>></span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div><div style="font-size:10pt;font-family:verdana,helvetica,sans-serif"><div>Ho un problema banale con il modulo "argparse".</div><div>Mi serve implementare semplicemente.</div><div><br></div><div>comando valore</div>
<div>comando -v(flag opzionale. Mi stampa la versione ed esce) <br></div><div><br></div><div>Ho provato così:</div><div><br></div><div>parser = argparse.ArgumentParser()<br>parser.add_argument("strings",metavar="PATH", type=unicode,default="")<br>
parser.add_argument("-v", dest='vers',action="store_true", help="versione",required=False)<br>values=parser.parse_args()</div><div><br>if values.vers == True:<br> print "Versione"<br>
sys.exit(0)</div><div><br></div><div>Senza il flag "-v" funziona.</div><div>Con il "-v" mi risponde:</div><div>error: too few arguments</div><div><br></div><div>vuole per forza l'altro parametro.</div>
<div>Come faccio a saltarlo
?</div></div></div></blockquote><div><br></div><div>C'è qualcosa che mi sfugge.</div></div></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div class="gmail_extra"><div class="gmail_quote"><div><div>
<font face="'courier new', monospace">marcob@pc-beruntu:~/work/python$ cat arg.py</font></div></div></div></div><div class="gmail_extra"><div class="gmail_quote"><div><div><font face="'courier new', monospace">import argparse, sys</font></div>
</div></div></div><div class="gmail_extra"><div class="gmail_quote"><div><div><font face="'courier new', monospace">parser = argparse.ArgumentParser()</font></div></div></div></div><div class="gmail_extra"><div class="gmail_quote">
<div><div><font face="'courier new', monospace">parser.add_argument("strings",metavar="PATH", type=unicode,default="")</font></div></div></div></div><div class="gmail_extra"><div class="gmail_quote">
<div><div><font face="'courier new', monospace">parser.add_argument("-v", dest='vers',action="store_true", help="versione",required=False)</font></div></div></div></div><div class="gmail_extra">
<div class="gmail_quote"><div><div><font face="'courier new', monospace">values=parser.parse_args()</font></div></div></div></div><div class="gmail_extra"><div class="gmail_quote"><div><div><font face="'courier new', monospace"><br>
</font></div></div></div></div><div class="gmail_extra"><div class="gmail_quote"><div><div><font face="'courier new', monospace">if values.vers == True:</font></div></div></div></div><div class="gmail_extra"><div class="gmail_quote">
<div><div><font face="'courier new', monospace"> print "Versione"</font></div></div></div></div><div class="gmail_extra"><div class="gmail_quote"><div><div><font face="'courier new', monospace"> sys.exit(0)</font></div>
</div><div><font face="'courier new', monospace"><br></font></div></div></div><div class="gmail_extra"><div class="gmail_quote"><div><div><font face="'courier new', monospace">marcob@pc-beruntu:~/work/python$ python arg.py -v </font></div>
</div></div></div><div class="gmail_extra"><div class="gmail_quote"><div><div><font face="'courier new', monospace">usage: arg.py [-h] [-v] PATH</font></div></div></div></div><div class="gmail_extra"><div class="gmail_quote">
<div><div><font face="'courier new', monospace">arg.py: error: too few arguments</font></div></div></div></div><div class="gmail_extra"><div class="gmail_quote"><div><div><font face="'courier new', monospace"><br>
</font></div><div><font face="'courier new', monospace">marcob@pc-beruntu:~/work/python$ python arg.py </font></div></div></div></div><div class="gmail_extra"><div class="gmail_quote"><div><div><font face="'courier new', monospace">usage: arg.py [-h] [-v] PATH</font></div>
</div></div></div><div class="gmail_extra"><div class="gmail_quote"><div><div><font face="'courier new', monospace">arg.py: error: too few arguments</font></div></div></div></div></blockquote><div class="gmail_extra">
<div class="gmail_quote"><div><br></div></div><div class="gmail_extra"><br></div><div class="gmail_extra">A me pare che non funzioni anche senza argomenti...</div><div class="gmail_extra">Se stampi l'help vedi che l'argomento PATH, in quanto posizionale, è obbligatorio:</div>
<div class="gmail_extra"><br></div></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div class="gmail_extra"><div class="gmail_extra"><div class="gmail_extra"><font face="'courier new', monospace">marcob@pc-beruntu:~/work/python$ python arg.py -h</font></div>
</div></div><div class="gmail_extra"><div class="gmail_extra"><div class="gmail_extra"><font face="'courier new', monospace">usage: arg.py [-h] [-v] PATH</font></div></div></div><div class="gmail_extra"><div class="gmail_extra">
<div class="gmail_extra"><font face="'courier new', monospace"><br></font></div></div></div><div class="gmail_extra"><div class="gmail_extra"><div class="gmail_extra"><font face="'courier new', monospace">positional arguments:</font></div>
</div></div><div class="gmail_extra"><div class="gmail_extra"><div class="gmail_extra"><font face="'courier new', monospace"> PATH</font></div></div></div><div class="gmail_extra"><div class="gmail_extra"><div class="gmail_extra">
<font face="'courier new', monospace"><br></font></div></div></div><div class="gmail_extra"><div class="gmail_extra"><div class="gmail_extra"><font face="'courier new', monospace">optional arguments:</font></div>
</div></div><div class="gmail_extra"><div class="gmail_extra"><div class="gmail_extra"><font face="'courier new', monospace"> -h, --help show this help message and exit</font></div></div></div><div class="gmail_extra">
<div class="gmail_extra"><div class="gmail_extra"><font face="'courier new', monospace"> -v versione</font></div></div></div></blockquote><div class="gmail_extra"><div class="gmail_extra"><div><br></div>
</div><div class="gmail_extra">Ciao.</div><div class="gmail_extra">Marco.</div><div class="gmail_extra"><br></div>-- <br><div><div><div><div><a href="http://beri.it/" target="_blank">http://beri.it/</a> - Un blog</div><div>
<a href="http://beri.it/i-miei-libri/" target="_blank">http://beri.it/i-miei-libri/</a> - Qualche libro</div><div><br></div></div></div></div><br>
</div>