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.0.tar.gz (18.1 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.0-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: json_arg-0.1.0.tar.gz
  • Upload date:
  • Size: 18.1 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.0.tar.gz
Algorithm Hash digest
SHA256 03f9c28368b640d0903731aeacf8869b21d7622824cd6579bcb18f2058c104a1
MD5 ee0d46e96e5ae71d342c26aff8c6fa50
BLAKE2b-256 1cb50db394d0f765af56bcb2b2016488323362cea7f91291a5feb7eeeaba4666

See more details on using hashes here.

File details

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

File metadata

  • Download URL: json_arg-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 11.1 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bf6bcef2dd183efc977b593bc5f95892e34337b6ab6a9dd5007679f1c276d988
MD5 40dd9f3fdc161e404db3ae126a04e43e
BLAKE2b-256 ab3f686ce64e2651c3b1e8a7aeaf415a70b9f01f9dbcc8847bdbd07a92a775d4

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