Skip to main content

convert object/class to CLI command

Project description

Install

  • pip install obj2cli

Usage(single)

import sys
import obj2cli


class A:
    def __init__(self, arg1, arg2):
        self.a = arg1
        self.b = arg2

    def call1(self, c, d=None):
        print("hello {} c={}, d={}".format(self.__dict__, c, d))


if __name__ == "__main__":
    obj2cli.main_single(A, A, sys.argv[1:])
# python cmd.py --help
usage: A [-h] [--arg1 str] [--arg2 str] {call1} ...

Initialize self. See help(type(self)) for accurate signature.

positional arguments:
  {call1}
    call1

optional arguments:
  -h, --help  show this help message and exit
  --arg1 str
  --arg2 str

# python cmd.py call1 --help
usage: A call1 [-h] [-c str] [-d str]

optional arguments:
  -h, --help  show this help message and exit
  -c str
  -d str

# python cmd.py --arg1 123 --arg2 345 call1 -c hello -d world
hello {'a': '123', 'b': '345'} c=hello, d=world

Usage(multi)

import sys
import obj2cli


class A:
    def __init__(self, a, b):
        self.a = a
        self.b = b

    def call1(self, c, d):
        print("hello {}: {}, c={}, d={}".format(self.__class__.__name__, self.__dict__, c, d))


class B(A):
    pass


if __name__ == "__main__":
    obj2cli.main_cls({A: A, B: B}, sys.argv[1:])

Usage(cli)

# obj2cli --module os.path --function basename -- --help
usage: basename [-h] [-p str]

Returns the final component of a pathname

optional arguments:
  -h, --help  show this help message and exit
  -p str

# obj2cli --module os.path --function basename -- -p /usr/bin/ls
ls

# obj2cli --module logging --function warning --positional -- --help
usage: warning [-h] str [str [str ...]]

Log a message with severity 'WARNING' on the root logger. If the logger has no
handlers, call basicConfig() to add a console handler with a pre-defined format.

positional arguments:
  str
  str

optional arguments:
  -h, --help  show this help message and exit
# obj2cli --module logging --function warning --positional -- "hello %s" "world"
2020-01-04 23:16:05,264 WARNING hello world

Links

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

obj2cli-0.0.3-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file obj2cli-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: obj2cli-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.8.1

File hashes

Hashes for obj2cli-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 d022e5d1c6591835637a3f04d51282856eb1c9ab3e89e0b324e03a203b5c8d18
MD5 e7f1d92a9c262b492cc255e1a535ca86
BLAKE2b-256 678ee8be90fcc0e42ef7aadd1c779c4a8ec12225973ab2944dc96466a0ec084f

See more details on using hashes here.

Supported by

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