Diferències
Ací es mostren les diferències entre la revisió seleccionada i la versió actual de la pàgina.
| Ambdós costats versió prèvia Revisió prèvia Següent revisió | Revisió prèvia | ||
| development:python:argparse [25/09/2025 23:11] – mate | development:python:argparse [26/09/2025 01:05] (actual) – mate | ||
|---|---|---|---|
| Línia 1: | Línia 1: | ||
| = Python argparse | = Python argparse | ||
| - | * [[https://docs.python.org/ | + | * [[https://ellibrodepython.com/python-argparse]] |
| == argparse | == argparse | ||
| + | * o = argparse.ArgumentParser: | ||
| + | * description: | ||
| + | * prefix_chars: | ||
| + | * fromfile_prefix_chars: | ||
| + | * version: numero de versión | ||
| + | * g = o.add_mutually_exclusive_group(): | ||
| + | * g.add_argument | ||
| + | * o.add_argument | ||
| + | * '' | ||
| + | * '' | ||
| + | * type: tipo int, str... | ||
| + | * help: texto de ayuda | ||
| + | * choices: lista de opciones válidas | ||
| + | * default: valor por defecto si no se pasa el parámetro | ||
| + | * required: True|False | ||
| + | * dest: cambia el nombre de la variable donde se almacena el valor | ||
| + | * nargs | ||
| + | * n: número de argumentos | ||
| + | * ''?'': | ||
| + | * '' | ||
| + | * '' | ||
| + | * action | ||
| + | * store: Es el comportamiento por defecto que hemos visto anteriormente. Simplemente almacena el valor que se pasa con el argumento en una variable. | ||
| + | * store_const: | ||
| + | * store_true: Almacena el booleano True en la variable. Muy útil para definir flags que no reciben un valor concreto. | ||
| + | * store_false: | ||
| + | * append: Añade el argumento a una lista. Útil cuando un argumento es pasado múltiples veces. | ||
| + | * append_const: | ||
| + | * count: Cuenta el número de veces que un determinado argumento es pasado. | ||
| + | * help: Muestra la ayuda del programa y finaliza sin hacer nada más. | ||
| + | * version: Muestra la versión del programa y finaliza la ejecución. | ||
| + | * se pueden crear acciones personalizadas con '' | ||
| + | |||
| + | == ejemplos | ||
| + | <code python> | ||
| + | # calculadora.py | ||
| + | import argparse | ||
| + | |||
| + | parser = argparse.ArgumentParser(description=' | ||
| + | parser.add_argument(' | ||
| + | parser.add_argument(' | ||
| + | parser.add_argument(' | ||
| + | type=str, | ||
| + | choices=[' | ||
| + | default=' | ||
| + | help=' | ||
| + | |||
| + | args = parser.parse_args() | ||
| + | </ | ||
| + | |||
| + | <code python> | ||
| + | # cambio_prefijo.py | ||
| + | import argparse | ||
| + | |||
| + | parser = argparse.ArgumentParser(prefix_chars=' | ||
| + | parser.add_argument(' | ||
| + | parser.add_argument(' | ||
| + | |||
| + | args = parser.parse_args() | ||
| + | print(vars(args)) | ||
| + | </ | ||
| + | |||
| + | <code python argumentos_fichero.py> | ||
| + | # argumentos_fichero.py | ||
| + | import argparse | ||
| + | |||
| + | parser = argparse.ArgumentParser(fromfile_prefix_chars=' | ||
| + | |||
| + | parser.add_argument(' | ||
| + | parser.add_argument(' | ||
| + | parser.add_argument(' | ||
| + | parser.add_argument(' | ||
| + | parser.add_argument(' | ||
| + | parser.add_argument(' | ||
| + | |||
| + | args = parser.parse_args() | ||
| + | print(vars(args)) | ||
| + | |||
| + | # argumentos_fichero.py @args.txt | ||
| + | </ | ||
| + | |||
| + | <code python excluyentes.py> | ||
| + | # excluyentes.py | ||
| + | import argparse | ||
| + | |||
| + | parser = argparse.ArgumentParser() | ||
| + | grupo = parser.add_mutually_exclusive_group() | ||
| + | |||
| + | grupo.add_argument(' | ||
| + | grupo.add_argument(' | ||
| + | |||
| + | args = parser.parse_args() | ||
| + | print(vars(args)) | ||
| + | </ | ||
| + | |||
| + | <code python argumentos_variables1.py> | ||
| + | # help_version.py | ||
| + | import argparse | ||
| + | parser = argparse.ArgumentParser() | ||
| + | parser.version = ' | ||
| + | parser.add_argument(' | ||
| + | parser.add_argument(' | ||
| + | args = parser.parse_args() | ||
| + | print(vars(args)) | ||
| + | </ | ||
| + | |||
| + | <code python argumentos_variables1.py> | ||
| + | # argumentos_variables1.py | ||
| + | import argparse | ||
| + | parser = argparse.ArgumentParser() | ||
| + | parser.add_argument(' | ||
| + | args = parser.parse_args() | ||
| + | print(vars(args)) | ||
| + | </ | ||
| + | |||
| + | <code python argumentos_variables2.py> | ||
| + | # argumentos_variables2.py | ||
| + | import argparse | ||
| + | parser = argparse.ArgumentParser() | ||
| + | parser.add_argument(' | ||
| + | args = parser.parse_args() | ||
| + | print(vars(args)) | ||
| + | </ | ||
| + | |||
| + | <code python argumentos_variables3.py> | ||
| + | # argumentos_variables3.py | ||
| + | import argparse | ||
| + | parser = argparse.ArgumentParser() | ||
| + | parser.add_argument(' | ||
| + | args = parser.parse_args() | ||
| + | print(vars(args)) | ||
| + | </ | ||
| + | |||
| + | <code python argumentos_variables4.py> | ||
| + | # argumentos_variables4.py | ||
| + | import argparse | ||
| + | parser = argparse.ArgumentParser() | ||
| + | parser.add_argument(' | ||
| + | args = parser.parse_args() | ||
| + | print(vars(args)) | ||
| + | </ | ||
| + | |||
| + | <code python argumentos_variables5.py> | ||
| + | # argumentos_variables5.py | ||
| + | import argparse | ||
| + | parser = argparse.ArgumentParser() | ||
| + | parser.add_argument(' | ||
| + | parser.add_argument(' | ||
| + | args = parser.parse_args() | ||
| + | print(vars(args)) | ||
| + | </ | ||
| + | |||
| + | == ejemplos | ||
| + | /via: [[https:// | ||
| <code python> | <code python> | ||
| # | # | ||