Skip to main content

Load json as args and save args to json. Convert arg parser to json-schema and convert json-schema to arg parser

Project description

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

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

json_arg-0.1.1.tar.gz (18.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

json_arg-0.1.1-py3-none-any.whl (11.2 kB view details)

Uploaded Python 3

File details

Details for the file json_arg-0.1.1.tar.gz.

File metadata

  • Download URL: json_arg-0.1.1.tar.gz
  • Upload date:
  • Size: 18.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.11.9 Windows/10

File hashes

Hashes for json_arg-0.1.1.tar.gz
Algorithm Hash digest
SHA256 e1564e5765bdd944f79f03f9d9c6e0b2b913003308e320d79a45a536d36ce52c
MD5 59755815011426b9a879f7578e73d609
BLAKE2b-256 656205b6c31ef75f69b4f846e045994cea25f4df62ca57199687ba5ee2d84472

See more details on using hashes here.

File details

Details for the file json_arg-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: json_arg-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 11.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.11.9 Windows/10

File hashes

Hashes for json_arg-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 188a92a02bfd23ec30f11981ca8cfb0b03c13c0d5ac65145042f4b757feaed7c
MD5 954567429391c7e68e872fed686791c5
BLAKE2b-256 9a7542c81236908030d2984d635eb57329d9e3f864e2f1c6493c910ebc58c78a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page