Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (
Help us improve Python packaging - Donate today!

traitscli - CLI generator based on class traits

Project Description

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.


pip install traitscli


  • traits
  • argparse (for Python < 2.7)


Source code:

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

class SampleCLI(TraitsCLIBase):

    Sample CLI using `traitscli`.


      %(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

Example run:

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

Sample CLI using `traitscli`.

Example:: --yes                # => obj.yes = True --string something   # => obj.string = 'string' --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 --yes --choice a
string : ''
no     : True
fnum   : 0.0
choice : 'a'
inum   : 0
yes    : True

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

Release History

This version
History Node


History Node


Download Files

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

Filename, Size & Hash SHA256 Hash Help File Type Python Version Upload Date
(10.9 kB) Copy SHA256 Hash SHA256
Source None Mar 22, 2013

Supported By

Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Google Google Cloud Servers DreamHost DreamHost Log Hosting