JSON argparse
Feature
- Load json as args
- Save args to json
- Convert arg parser to json-schema (fork and modify from argparse-to-json)
- Convert json-schema to arg parser (fork and modify from argparse-schema)
Install
pip install json-arg
Or use poetry
poetry add json-arg
Usage
Setup a parser
Set up a parser and wrap it
import argparse
from json_arg import Parser
parser = argparse.ArgumentParser()
parser.add_argument("name")
json_argparser = Parser(parser)
# use `json_argparser.parser` to get the wrapped one
Set up a parser from json-schema
from json_arg import Parser
# Use dict as json
json_schema: dict = {}
json_argparser = Parser(schema=json_schema)
# Or from a path
json_schema_path = "./config.json"
json_argparser = Parser(schema=json_schema_path)
Add json-schema to existed parser
json_schema_path = "./config.json"
json_argparser.add_json_schema(schema = json_schema_path)
Load json as args
The format of json you can find which in saving args firstly.
Add argument --json-load for load json as args:
json_argparser.add_argument_json_load()
# get args result
args = json_argparser.parse_args()
Using in commandline is like below.
python main.py --json-load ./config.json
Save args to json
Add argument --json-save for save args as json:
json_argparser.add_argument_json_save()
Using in commandline, you just add the --json-save /path/file.json after the command.
Otherwise, if you just add --json-save, it will be saved in ./run_config.json.
python main.py --other argument --json-save ./config.json
Then you will find the json file in config.json
{
"other": "argument"
}
Convert arg parser to json-schema
To dict.
from json_arg import Parser
# wrapped the existed parser
json_argparser = Parser(parser)
json_args = json_argparser.to_json_schema()
json_argparser.save_json_schema()
To string.
json_args = json_argparser.save_json_schema()
Save as json-schema file.
json_argparser.save_json_schema("./file.json")
TODO List:
- The test of argparse-to-schema and schema-to-argparse
- The test of load-from-json and save-to-json
- The positional argument of argparse-to-schema,
- The subcommand support of schema-to-argparse
- The subcommand support of load-from-json
- Support nargs in schema
- More tests in nested subcommand of argparse-to-schema
Metadata
Release files for json-arg 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| json_arg-0.1.2.tar.gz | 18.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| json_arg-0.1.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 29.5 kB
Release files / json_arg-0.1.2.tar.gz
| Download URL | json_arg-0.1.2.tar.gz |
|---|---|
| Size | 18.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e7bb3b92f0ea34bbd8e72f5e42c298b92ce1231d380e314a2dfd51c352cc7288
|
|
BLAKE2b-256 checksum How to use checksums |
06e210dfe04e2d2f3079ccfbf193425a92b731107eff67c4dbba428e743591ec
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.5 CPython/3.11.9 Windows/10
|
Release files / json_arg-0.1.2-py3-none-any.whl
| Download URL | json_arg-0.1.2-py3-none-any.whl |
|---|---|
| Size | 11.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1a6c09f198e3059fa79360522fd10c326086a56b6a42fbd87097a4c5675d8497
|
|
BLAKE2b-256 checksum How to use checksums |
5545a1834fb667b93363138ee0823feb3db08a743f5825d76534cb8f2ffe76a8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.5 CPython/3.11.9 Windows/10
|