Skip to content

Commit

Permalink
Merge pull request #23 from olin/cli-cleanup
Browse files Browse the repository at this point in the history
CLI Cleanup
  • Loading branch information
newsch authored Oct 21, 2019
2 parents f89635f + fc9b2e5 commit e5f2147
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 23 deletions.
30 changes: 19 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ this by cloning the source repository to somewhere on your machine and running
`pip install`:

```shell
git clone https://github.com/olin/focstest.git
git clone --branch=release https://github.com/olin/focstest.git # clone a stable version
pip install focstest/
```

Expand Down Expand Up @@ -119,15 +119,14 @@ refresh `focstest`'s copy, use the `--update-cache` flag:
focstest homework2.ml --update-cache
```


`focstest` uses a standard python-powered command-line interface. You can always
ask it for help with `--help` or `-h`.

```
```shell
$ focstest --help
usage: focstest [-h] [-u URL] [-v] [--log-level {debug,info,warning}] [-uc]
[-U [N [N ...]] | -S [N [N ...]]]
ocaml-file
usage: focstest [-h] [--version] [--url URL] [-v] [-uc] [-u [N [N ...]] | -s
[N [N ...]]]
ocaml-file

Run ocaml "doctests".

Expand All @@ -136,15 +135,17 @@ positional arguments:

optional arguments:
-h, --help show this help message and exit
-u URL, --url URL a url to scrape tests from
--version show program's version number and exit
--url URL a url to scrape tests from (usually automagically
guessed from ocaml-file)
-v, --verbose increase test output verbosity
--log-level {debug,info,warning}
the program log level
-uc, --update-cache update cached files
-U [N [N ...]], --use-suites [N [N ...]]
-u [N [N ...]], --use-suites [N [N ...]]
test suites to use exclusively, indexed from 1
-S [N [N ...]], --skip-suites [N [N ...]]
-s [N [N ...]], --skip-suites [N [N ...]]
test suites to skip, indexed from 1
Submit bugs to <https://github.com/olin/focstest/issues/>.
```
For most homeworks, the workflow that I've used is going question by question
Expand Down Expand Up @@ -181,3 +182,10 @@ Run `pipenv install --dev` to install all of the dev packages.
Run tests with `python -m unittest discover`.
Want to use it while you hack on it? Install it with `pip install -e`.
You can set `focstest`'s logging level with the `LOG_LEVEL` environment variable.
The possible values are all of python's usual logging levels, set it to `DEBUG`
for more output.
```shell
$ LOG_LEVEL=DEBUG focstest homework3.ml
```
29 changes: 17 additions & 12 deletions focstest.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,35 +196,40 @@ def main():
epilog='Submit bugs to <https://github.com/olin/focstest/issues/>.')
parser.add_argument('--version', action='version', version=__version__)
input_types = parser.add_mutually_exclusive_group(required=False)
input_types.add_argument('-u', '--url', type=str,
help='a url to scrape tests from (usually automagically guessed based on ocaml-file)')
# input_types.add_argument('-f', '--file', type=str,
# help='a file to load tests from')
input_types.add_argument('--url', type=str,
help='a url to scrape tests from (usually automagically guessed from ocaml-file)')
parser.add_argument('ocaml-file', type=str,
help='the ocaml file to test against')
parser.add_argument('-v', '--verbose', action='store_true',
help='increase test output verbosity')
parser.add_argument('--log-level', choices=['debug', 'info', 'warning'],
help='the program log level')
parser.add_argument('-uc', '--update-cache', action='store_true',
help='update cached files')
test_selection = parser.add_mutually_exclusive_group(required=False)
test_selection.add_argument('-U', '--use-suites', metavar='N', type=int, nargs='*',
test_selection.add_argument('-u', '--use-suites', metavar='N', type=int, nargs='*',
help='test suites to use exclusively, indexed from 1')
test_selection.add_argument('-S', '--skip-suites', metavar='N', type=int, nargs='*',
test_selection.add_argument('-s', '--skip-suites', metavar='N', type=int, nargs='*',
help='test suites to skip, indexed from 1')
args = parser.parse_args()
if args.log_level:
numeric_level = getattr(logging, args.log_level.upper(), None)
logger.setLevel(numeric_level)

# check environment var for logging level
log_level = os.getenv('LOG_LEVEL')
if log_level is not None:
log_level = log_level.upper()
try:
numeric_level = getattr(logging, os.getenv('LOG_LEVEL'))
except AttributeError as e:
logging.warning("Found 'LOG_LEVEL' env var, but was unable to parse: {}".format(e))
else:
logger.setLevel(numeric_level)
logger.debug('Set logging level to {!r} ({}) from env var'.format(log_level, numeric_level))

URL = args.url
FILE = getattr(args, 'ocaml-file')

if not args.url:
url_guess = infer_url(FILE)
if not url_guess: # break if filename can't be matched
logger.critical('Could not infer url from filename {!r}. Try passing a url manually with the `-u`/`--url` flag.'.format(FILE))
logger.critical('Could not infer url from filename {!r}. Try passing a url manually with the `--url` flag.'.format(FILE))
sys.exit(1)
else:
URL = url_guess
Expand Down

0 comments on commit e5f2147

Please sign in to comment.