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.4-py3-none-any.whl (12.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: obj2cli-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 12.3 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.42.0 CPython/3.8.1

File hashes

Hashes for obj2cli-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 792332f0d3ec4532f6b2ce6ead7c8fb0573d69d9c7d36087d0bcaa5d999235ac
MD5 221d1536ab54e1dd54d000126e62e07b
BLAKE2b-256 c90463fee3f806da2c0406b68a621e7aff5525a4175c0f3718fc1f9f5d30999d

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