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.2.tar.gz (18.2 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.2-py3-none-any.whl (11.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: json_arg-0.1.2.tar.gz
  • Upload date:
  • Size: 18.2 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.2.tar.gz
Algorithm Hash digest
SHA256 e7bb3b92f0ea34bbd8e72f5e42c298b92ce1231d380e314a2dfd51c352cc7288
MD5 596673eecd6f358c886680a960a83dad
BLAKE2b-256 06e210dfe04e2d2f3079ccfbf193425a92b731107eff67c4dbba428e743591ec

See more details on using hashes here.

File details

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

File metadata

  • Download URL: json_arg-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 11.3 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1a6c09f198e3059fa79360522fd10c326086a56b6a42fbd87097a4c5675d8497
MD5 990987997ce1df2f0d67b7f5dfd672a1
BLAKE2b-256 5545a1834fb667b93363138ee0823feb3db08a743f5825d76534cb8f2ffe76a8

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