Skip to content

Commit

Permalink
Merge 8cc457a into e6a7dd9
Browse files Browse the repository at this point in the history
  • Loading branch information
shakefu committed Dec 11, 2018
2 parents e6a7dd9 + 8cc457a commit 54a53a3
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 11 deletions.
43 changes: 35 additions & 8 deletions .travis.yml
@@ -1,13 +1,40 @@
matrix:
include:
- os: linux
dist: trusty
# sudo: false
python: '2.7'
- os: linux
dist: trusty
# sudo: false
python: '3.5'
- os: linux
dist: trusty
# sudo: false
python: '3.6'
- os: linux
dist: xenial
# sudo: required
python: '3.7'
- os: linux
# sudo: false
python: pypy
- os: linux
# sudo: false
python: pypy3
- os: linux
dist: trusty
# sudo: false
env: EXTRAS=configargparse
python: '2.7'
- os: linux
dist: xenial
# sudo: required
env: EXTRAS=configargparse
python: '3.7'
language: python
python:
- "2.7"
# - "3.2"
- "3.3"
- "3.4"
- "3.5"
- "3.6"
install:
- pip install .
- pip install coveralls coverage
- pip install coveralls coverage nose mock $EXTRAS
script: coverage run --source=pytool setup.py test
after_success: coveralls
28 changes: 26 additions & 2 deletions pytool/cmd.py
Expand Up @@ -6,7 +6,14 @@

import sys
import signal
import argparse

# Handle the optional configargparse lib
try:
import configargparse as argparse
HAS_CAP = True
except:
import argparse
HAS_CAP = False

import pytool.text
try:
Expand Down Expand Up @@ -108,10 +115,27 @@ def run(self):
"""
def __init__(self):
self.parser = argparse.ArgumentParser(add_help=False)
self.parser = argparse.ArgumentParser(add_help=False,
**self.parser_opts())
self.set_opts()
self.opt('--help', action='help', help='display this help and exit')

def parser_opts(self):
""" Subclasses should override this method to return a dictionary of
additional arguments to the parser instance.
**Example**::
class MyCommand(Command):
def parser_opts(self):
return dict(
description="Manual description for cmd.",
auto_env_var_help=True,
auto_env_var_prefix=True,
)
"""
return dict()

def set_opts(self):
""" Subclasses should override this method to configure the command
line arguments and options.
Expand Down
1 change: 1 addition & 0 deletions setup.py
Expand Up @@ -44,6 +44,7 @@ def version():
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Topic :: Software Development :: Libraries',
'Topic :: Utilities',
]
Expand Down
34 changes: 33 additions & 1 deletion test/test_cmd.py
@@ -1,9 +1,10 @@
import os
import sys

import mock

import pytool
from .util import eq_, raises
from .util import eq_, raises, SkipTest


class TestCommand(pytool.cmd.Command):
Expand Down Expand Up @@ -46,3 +47,34 @@ def test_stop(exit):
def test_console_script(start):
TestCommand().console_script()
start.assert_called_with(sys.argv[1:])


class test_configargparse():
def setup(self):
if not pytool.cmd.HAS_CAP:
raise SkipTest

class Cmd(pytool.cmd.Command):
def parser_opts(self):
return dict(auto_env_var_prefix='test_')

def set_opts(self):
self.opt('--test', action='store_true',
help='This is my test option.')
self.opt('--config', '-c', is_config_file=True)

def run(self):
pass

self.Cmd = Cmd

@mock.patch.dict(os.environ, {'TEST_TEST': 'true'})
def test_env_var(self):
cmd = self.Cmd()
cmd.start([])
eq_(cmd.args.test, True)

def test_conf_file(self):
cmd = self.Cmd()
cmd.start(['-c', 'test/test_conf.yml'])
eq_(cmd.args.test, True)
1 change: 1 addition & 0 deletions test/test_conf.yml
@@ -0,0 +1 @@
test: true

0 comments on commit 54a53a3

Please sign in to comment.