Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/audy/esrapgra
The opposite of argparse
https://github.com/audy/esrapgra
Last synced: about 18 hours ago
JSON representation
The opposite of argparse
- Host: GitHub
- URL: https://github.com/audy/esrapgra
- Owner: audy
- License: mit
- Created: 2017-09-24T06:51:56.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2023-10-22T17:19:36.000Z (12 months ago)
- Last Synced: 2024-09-02T09:48:06.709Z (about 1 month ago)
- Language: Python
- Homepage: https://pypi.org/project/esrapgra/
- Size: 19.5 KB
- Stars: 6
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# Esrapgra
[![Test](https://github.com/audy/esrapgra/actions/workflows/test.yml/badge.svg)](https://github.com/audy/esrapgra/actions/workflows/test.yml)
The opposite of argparse. Converts function arguments to command-line
arguments.## Installation
```
pip install esrapgra
```From source
```
pip install git+https://github.com/audy/esrapgra
```## Overview
So you want to call `wget` from Python, mapping Python variables to
command-line arguments:```sh
wget \
http://example.com/file.zip \
--directory-prefix=/path/to/directory \
--retry-connrefused \
--waitretry=5 \
--tries=3
```This often gets messy if you need to interpolate values into the arguments or
map Python logic into command logic:```python
args = [
"http://example.com/file.zip",
"--directory-prefix",
directory_prefix,
f"--tries={tries}"
]if retry_connrefused:
args.append("--retry-connrefused")subprocess.check_output(["wget"] + args)
```With Esrapgra (if you can remember how to spell it), this becomes a nice
function invocation:```python
es = Esrapgra(kwargs_last=True)args = es.lex_arguments(
"http://example.com/file.zip",
directory_prefix="/path/to/directory",
retry_connrefused=True,
waitretry=5,
tries=3
)subprocess.check_output(["wget"] + args)
```The following things are happening:
- String arguments are automatically escaped and quote to protect against
shell-escape attacks
- Boolean arguments are automatically turned into boolean flags (e.g.,
`retry_connrefused=True` turns into `--retry-connrefused` and
`retry_connrefused=False` turns into nothing)
- Numeric flags are automatically converted into strings## Examples
### Basic Initialization
You can initialize the `Esrapgra` object with default parameters:
```python
esrap = Esrapgra()
```### Custom Separator
Customize the separator between words in keyword arguments:
```python
# Using underscores as separators
esrap = Esrapgra(separator="_")
``````python
# Example usage
esrap.lex_arguments(test_argument="test") # Output: ['--test_argument="test"']
```### Custom Prefix
Define a custom prefix for the keyword arguments:
```python
# Using a single hyphen as a prefix
esrap = Esrapgra(prefix="-")
``````python
# Example usage
esrap.lex_arguments(test_argument="test") # Output: ['-test_argument="test"']
```### Keyword Arguments Position
Control the position of keyword arguments relative to positional arguments:
```python
# Putting keyword arguments before positional arguments
esrap = Esrapgra(kwargs_last=False)
``````python
# Example usage
esrap.lex_arguments(
"first_arg",
test_argument="test"
)
# Output: ['-test_argument="test"', 'first_arg']
```### Quoting Strings
You can enable or disable the quoting of strings as per your needs:
```python
# Disabling quoting of strings
esrap = Esrapgra(quote_strings=False)
``````python
# Example usage# Adjust the expected output based on the implementation of `lex_arguments`
esrap.lex_arguments(test_argument="test")
```