Easy way to use named arguments by means of python dictionaries
Project description
FriendlyArguments
Uma biblioteca Python simples e flexível para parsing de argumentos de linha de comando.
Instalação
pip install friendly-arguments
Características
- Sintaxe flexível: Suporta
--arg=value,--arg value,-a value - Flags booleanas:
--verbose,--debugretornamTrue - Valores padrão: Defina defaults facilmente
- Aliases: Suporte para nomes curtos e longos (
-ne--name) - Simples: API minimalista e intuitiva
- Zero dependências: Usa apenas a biblioteca padrão do Python
- Retrocompatível: Mantém a API antiga funcionando
📖 Uso Básico
Exemplo 1: Uso Simples
from friendly_arguments import get_args
# Parse todos os argumentos automaticamente
args = get_args()
# Acesse os valores
name = args.get('--name', 'World')
print(f"Hello, {name}!")
Execute:
python script.py --name João
# Output: Hello, João!
python script.py --name=Maria
# Output: Hello, Maria!
python script.py
# Output: Hello, World!
Exemplo 2: Com Valores Padrão
from friendly_arguments import get_args
# Defina valores padrão
args = get_args(defaults={
'--name': 'Anonymous',
'--age': '18',
'--city': 'Unknown'
})
print(f"Name: {args['--name']}")
print(f"Age: {args['--age']}")
print(f"City: {args['--city']}")
Execute:
python script.py --name João --age 25
# Output:
# Name: João
# Age: 25
# City: Unknown
Exemplo 3: Flags Booleanas
from friendly_arguments import get_args
args = get_args()
# Flags sem valor retornam True
verbose = args.get('--verbose', False)
debug = args.get('--debug', False)
if verbose:
print("Modo verbose ativado!")
if debug:
print("Modo debug ativado!")
Execute:
python script.py --verbose --debug
# Output:
# Modo verbose ativado!
# Modo debug ativado!
Exemplo 4: Aliases (Nomes Curtos e Longos)
from friendly_arguments import get_args, get_arg
args = get_args()
# Busca --name OU -n (o que encontrar primeiro)
name = get_arg(args, '--name', '-n', default='World')
# Busca --verbose OU -v
verbose = get_arg(args, '--verbose', '-v', default=False)
if verbose:
print(f"Hello, {name}!")
Execute:
python script.py -n João -v
# Output: Hello, João!
python script.py --name Maria --verbose
# Output: Hello, Maria!
Exemplo 5: Todas as Sintaxes Suportadas
from friendly_arguments import get_args
args = get_args()
# Todas essas formas funcionam:
# python script.py --name=João
# python script.py --name João
# python script.py -n João
# python script.py --verbose
# python script.py --city "São Paulo"
Retrocompatibilidade
A versão antiga ainda funciona para não quebrar código existente:
from friendly_arguments import get_params_sys_args
# API antiga (ainda funciona)
my_args = get_params_sys_args(['--text='])
text = my_args.get('--text=', 'default')
print(text)
Execute:
python script.py --text=hello
# Output: hello
Documentação da API
get_args(defaults=None)
Parse todos os argumentos da linha de comando.
Parâmetros:
defaults(dict, opcional): Dicionário com valores padrão
Retorna:
dict: Dicionário com os argumentos parseados
Exemplo:
args = get_args(defaults={'--port': '8080'})
get_arg(args, *keys, default=None)
Busca um argumento por múltiplos nomes possíveis.
Parâmetros:
args(dict): Dicionário retornado porget_args()*keys: Nomes de chaves para buscardefault: Valor padrão se nenhuma chave for encontrada
Retorna:
- O valor da primeira chave encontrada, ou
default
Exemplo:
args = get_args()
port = get_arg(args, '--port', '-p', default=8080)
get_params_sys_args(keys, silent=True)
Função legada para retrocompatibilidade.
Parâmetros:
keys(list): Lista de chaves esperadas com sufixo=silent(bool): SeTrue, não imprime valores (padrão:True)
Retorna:
dict: Dicionário com argumentos encontrados
Exemplos Práticos
Script de Configuração de Servidor
from friendly_arguments import get_args, get_arg
args = get_args(defaults={
'--host': 'localhost',
'--port': '8080'
})
host = get_arg(args, '--host', '-h')
port = get_arg(args, '--port', '-p')
debug = get_arg(args, '--debug', '-d', default=False)
print(f"Starting server on {host}:{port}")
if debug:
print("Debug mode enabled")
Script de Processamento de Dados
from friendly_arguments import get_args, get_arg
args = get_args()
input_file = get_arg(args, '--input', '-i')
output_file = get_arg(args, '--output', '-o')
verbose = get_arg(args, '--verbose', '-v', default=False)
if not input_file:
print("Error: --input is required")
exit(1)
if verbose:
print(f"Processing {input_file}...")
# Seu código aqui...
Testes
O pacote inclui uma suíte completa de testes unitários.
Executar os testes
# Executar todos os testes
python3 -m unittest discover tests -v
# Ou usar o script helper
python3 run_tests.py -v
Cobertura
- ✅ 49 testes unitários
- ✅ ~100% de cobertura do código
- ✅ Testes de integração
- ✅ Testes de casos extremos
Contribuindo
Contribuições são bem-vindas! Sinta-se à vontade para abrir issues ou pull requests.
Licença
MIT License - veja LICENSE.md para detalhes.
Autor
Horlando Leão
- GitHub: @Horlando-Leao
- Email: horlandojcleao.developer@gmail.com
Links
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file friendly_arguments-0.2.0.tar.gz.
File metadata
- Download URL: friendly_arguments-0.2.0.tar.gz
- Upload date:
- Size: 8.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
997040a73903dded8ecbd213c16d0d4c49dac02c1f472ed72b931e8aecb7ef36
|
|
| MD5 |
9425749c2cb53c9033b1b6b579c066e5
|
|
| BLAKE2b-256 |
59a51d2c832122bd2695fbfee73f9b2fca945ca09489ae2ad07aef42c6001e01
|
File details
Details for the file friendly_arguments-0.2.0-py3-none-any.whl.
File metadata
- Download URL: friendly_arguments-0.2.0-py3-none-any.whl
- Upload date:
- Size: 9.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cad7eb829c689bf9fb41165b6a37fd1f78f0cfb8df8fc3207334e34cb84a2d2d
|
|
| MD5 |
5b684d14af9702ae616d6578b6a23cf7
|
|
| BLAKE2b-256 |
3db0ab6751b62a7ca8c2dbcbb42dfeb0fe6be303dd1ecb9cd27d7365d8dd70d2
|