Re: [Python] Modulo argparse

2012-04-26 Per discussione Marco Beri
2012/4/26 Walter Valenti waltervale...@yahoo.it

 Ho un problema banale con il modulo argparse.
 Mi serve implementare semplicemente.

 comando valore
 comando -v(flag opzionale. Mi stampa la versione ed esce)

 Ho provato così:

 parser = argparse.ArgumentParser()
 parser.add_argument(strings,metavar=PATH, type=unicode,default=)
 parser.add_argument(-v, dest='vers',action=store_true,
 help=versione,required=False)
 values=parser.parse_args()

 if values.vers == True:
 print Versione
 sys.exit(0)

 Senza il flag -v funziona.
 Con il -v mi risponde:
 error: too few arguments

 vuole per forza l'altro parametro.
 Come faccio a saltarlo ?


C'è qualcosa che mi sfugge.

marcob@pc-beruntu:~/work/python$ cat arg.py
import argparse, sys
parser = argparse.ArgumentParser()
parser.add_argument(strings,metavar=PATH, type=unicode,default=)
parser.add_argument(-v, dest='vers',action=store_true,
help=versione,required=False)
values=parser.parse_args()

if values.vers == True:
print Versione
sys.exit(0)

marcob@pc-beruntu:~/work/python$ python arg.py  -v
usage: arg.py [-h] [-v] PATH
arg.py: error: too few arguments

marcob@pc-beruntu:~/work/python$ python arg.py
usage: arg.py [-h] [-v] PATH
arg.py: error: too few arguments



A me pare che non funzioni anche senza argomenti...
Se stampi l'help vedi che l'argomento PATH, in quanto posizionale, è
obbligatorio:

marcob@pc-beruntu:~/work/python$ python arg.py -h
usage: arg.py [-h] [-v] PATH

positional arguments:
  PATH

optional arguments:
  -h, --help  show this help message and exit
  -v  versione


Ciao.
Marco.

-- 
http://beri.it/ - Un blog
http://beri.it/i-miei-libri/ - Qualche libro
___
Python mailing list
Python@lists.python.it
http://lists.python.it/mailman/listinfo/python


Re: [Python] Modulo argparse

2012-04-26 Per discussione Walter Valenti
marcob@pc-beruntu:~/work/python$ python arg.py  -v 

usage: arg.py [-h] [-v] PATH
arg.py: error: too few arguments


marcob@pc-beruntu:~/work/python$ python arg.py 
usage: arg.py [-h] [-v] PATH
arg.py: error: too few arguments




A me pare che non funzioni anche senza argomenti...
Se stampi l'help vedi che l'argomento PATH, in quanto posizionale, è 
obbligatorio:


marcob@pc-beruntu:~/work/python$ python arg.py -h
usage: arg.py [-h] [-v] PATH


positional arguments:
  PATH


optional arguments:
  -h, --help  show this help message and exit
  -v          versione


Ciao.
Marco.



Quello che vorrei fare è il seguente

comando -v -- mi stampa la versione
comando path  -- esegue

In tutti gli altri casi visualizza l'help. 


E' poì che cerco nella documentazione ma non riesco a venirne a capo.

Walter

___
Python mailing list
Python@lists.python.it
http://lists.python.it/mailman/listinfo/python


Re: [Python] Modulo argparse

2012-04-26 Per discussione Riccardo Lemmi
Walter Valenti wrote:

marcob@pc-beruntu:~/work/python$ python arg.py  -v
 
usage: arg.py [-h] [-v] PATH
arg.py: error: too few arguments


marcob@pc-beruntu:~/work/python$ python arg.py
usage: arg.py [-h] [-v] PATH
arg.py: error: too few arguments




A me pare che non funzioni anche senza argomenti...
Se stampi l'help vedi che l'argomento PATH, in quanto posizionale, è
obbligatorio:


marcob@pc-beruntu:~/work/python$ python arg.py -h
usage: arg.py [-h] [-v] PATH


positional arguments:
PATH


optional arguments:
-h, --help  show this help message and exit
-v  versione


Ciao.
Marco.

 
 
 Quello che vorrei fare è il seguente
 
 comando -v -- mi stampa la versione
 comando path  -- esegue
 
 In tutti gli altri casi visualizza l'help.
 
 
 E' poì che cerco nella documentazione ma non riesco a venirne a capo.
 
 Walter

Prova:

parser.add_argument(strings, 
metavar=PATH, 
type=unicode, 
default=,
nargs='*')

-- 
   Riccardo Lemmi

___
Python mailing list
Python@lists.python.it
http://lists.python.it/mailman/listinfo/python


Re: [Python] Modulo argparse

2012-04-26 Per discussione Daniele Varrazzo

On Thu, 26 Apr 2012 15:41:25 +0100 (BST), Walter Valenti wrote:

Ho un problema banale con il modulo argparse.
Mi serve implementare semplicemente.

comando valore
comando -v(flag opzionale. Mi stampa la versione ed esce)


Ho provato così:

parser = argparse.ArgumentParser()
parser.add_argument(strings,metavar=PATH, 
type=unicode,default=)

parser.add_argument(-v, dest='vers',action=store_true,
help=versione,required=False)
values=parser.parse_args()

if values.vers == True:
    print Versione
    sys.exit(0)

Senza il flag -v funziona.
Con il -v mi risponde:
error: too few arguments

vuole per forza l'altro parametro.
Come faccio a saltarlo ?


Prova ad usare action='version' invece di store_true: probabilmente è 
fatto apposta per dire anche se ci sono parametri non facoltativi, 
sbattitene, stampa la versione ed esci. Con store_true, tutta la riga 
di comando deve essere validata.


http://docs.python.org/dev/library/argparse.html#action

--
Daniele Varrazzo - Develer S.r.l.
http://www.develer.com
___
Python mailing list
Python@lists.python.it
http://lists.python.it/mailman/listinfo/python