3
Hello, I’m trying to work with command line argument process, I’ll show you an example command line I’m using:
user@notebook$ python arquivo.py -nome Lucas --idade 12 --pais Brasil
Code:
import sys
import argparse
def main():
parser = argparse.ArgumentParser(description='Descricao')
parser.add_argument('-nome', required=True)
parser.add_argument('--idade', required=True)
parser.add_argument('--pais', required=True)
args = parser.parse_args()
print("Nome = {}".format(args.nome))
print("Idade = {}".format(args.idade))
print("País = {}".format(args.pais))
return 0
if __name__ == '__main__':
sys.exit(main())
Exit:
Nome = Lucas
Idade = 12
País = Brasil
I would like to know if there is a possibility of receiving, for example age without having to use the --age on the command line, so:
user@notebook$ python arquivo.py -nome Lucas 12 --pais Brasil
So I’d like the exit to stay:
Nome = Lucas
Idade = 12
País = Brasil
What happens if you take the
--
of--idade
in theadd_argument
?– Woss
By the way, why to display the name you put
args.arquivo
? For age you putargs.nome
?– Woss
Pardon friend, typo, in the question of the name "file"
– Afuro Terumi
In relation to
-
and--
it’s just a pattern I want to adopt, but it wouldn’t be necessary– Afuro Terumi