Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

Traits CLI - CLI generator based on class traits

Traits CLI is based on Enthought’s Traits library.

Some benefits:

  • Automatically set type (int/float/…) of command line argument.

  • Help string generation.

  • “Deep value”” configuration: e.g., --dict['a']['b']['c']=1 is equivalent to obj.dict['a']['b']['c'] = 1 in Python code.

  • Nested class configuration: e.g., --sub.attr=val is equivalent to obj.sub.attr = val in Python code.

  • Parameter file support (ini/conf, json, yaml, etc.). Load parameter from file then set attribute.

Installation

pip install traitscli

Dependencies

  • traits

  • argparse (for Python < 2.7)

Sample

Source code:

from traitscli import TraitsCLIBase
from traits.api import Bool, Float, Int, Str, Enum


class SampleCLI(TraitsCLIBase):

    '''
    Sample CLI using `traitscli`.

    Example::

      %(prog)s --yes                # => obj.yes = True
      %(prog)s --string something   # => obj.string = 'string'
      %(prog)s --choice x           # => raise error (x is not in {a, b, c})

    '''

    # These variables are configurable by command line option
    yes = Bool(desc='yes flag for sample CLI', config=True)
    no = Bool(True, config=True)
    fnum = Float(config=True)
    inum = Int(config=True)
    string = Str(config=True)
    choice = Enum(['a', 'b', 'c'], config=True)

    # You can have "internal" attributes which cannot be set via CLI.
    not_configurable_from_cli = Bool()

    def do_run(self):
        names = self.class_trait_names(config=True)
        width = max(map(len, names))
        for na in names:
            print "{0:{1}} : {2!r}".format(na, width, getattr(self, na))


if __name__ == '__main__':
    # Run command line interface
    SampleCLI.cli()

Example run:

$ python sample.py --help
usage: sample.py [-h] [--choice {a,b,c}] [--fnum FNUM] [--inum INUM] [--no]
                 [--string STRING] [--yes]

Sample CLI using `traitscli`.

Example::

  sample.py --yes                # => obj.yes = True
  sample.py --string something   # => obj.string = 'string'
  sample.py --choice x           # => raise error (x is not in {a, b, c})

optional arguments:
  -h, --help        show this help message and exit
  --choice {a,b,c}  (default: a)
  --fnum FNUM       (default: 0.0)
  --inum INUM       (default: 0)
  --no              (default: True)
  --string STRING   (default: )
  --yes             yes flag for sample CLI (default: False)

$ python sample.py --yes --choice a
string : ''
no     : True
fnum   : 0.0
choice : 'a'
inum   : 0
yes    : True

$ python sample.py --inum invalid_argument
usage: sample.py [-h] [--choice {a,b,c}] [--fnum FNUM] [--inum INUM] [--no]
                 [--string STRING] [--yes]
sample.py: error: argument --inum: invalid int value: 'invalid_argument'

Metadata

Release files for traitscli 0.1.beta0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for traitscli 0.1.beta0
File Size Uploaded Provenance
traitscli-0.1.beta0.tar.gz 10.8 kB Without provenance Details

Release files / traitscli-0.1.beta0.tar.gz

Download URL traitscli-0.1.beta0.tar.gz
Size 10.8 kB
Tags Source
SHA-256 checksum
How to use checksums
c07033ea9e27440e014eba917617f695cf1b1d1b17560f44132fe41215b7de8f
BLAKE2b-256 checksum
How to use checksums
e1911dffa948449677858aa4e4bb6b644a78c782bf54e9a7b638e566b0b70839
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for traitscli 0.1.beta0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page