Improve the dev CLI

This commit is contained in:
Danny Guo 2017-11-06 22:01:24 -05:00
parent e701012617
commit f4fd98e227
2 changed files with 39 additions and 14 deletions

View File

@ -77,9 +77,8 @@ fn main() {
```
### Development
If you don't want to install Rust itself, you can install [Docker], and run
`$ ./dev`. This should bring up a temporary container from which you can run
[cargo] commands.
If you don't want to install Rust itself, you can run `$ ./dev` for a
development CLI if you have [Docker] installed.
### License
[MIT](https://github.com/dguo/strsim-rs/blob/master/LICENSE)
@ -91,5 +90,3 @@ If you don't want to install Rust itself, you can install [Docker], and run
[Hamming]:http://en.wikipedia.org/wiki/Hamming_distance
[Optimal string alignment]:https://en.wikipedia.org/wiki/Damerau%E2%80%93Levenshtein_distance#Optimal_string_alignment_distance
[Docker]:https://docs.docker.com/engine/installation/
[cargo]:https://github.com/rust-lang/cargo

46
dev
View File

@ -1,13 +1,41 @@
#!/usr/bin/env python
from __future__ import print_function
#!/usr/bin/env python3
# ./dev --help
import argparse
import os
from subprocess import call
from subprocess import run
import sys
try:
call(['docker', 'run', '-it', '--rm', '-v', os.getcwd() + ':/src:cached',
'-w=/src', 'rust:1.20.0'])
except OSError:
print('Please install Docker.', file=sys.stderr)
parser = argparse.ArgumentParser(prog='./dev')
subparsers = parser.add_subparsers(metavar='<command>', title='commands')
command = [
'docker', 'run', '-it', '--rm', '-v', os.getcwd() + ':/src:cached',
'-w=/src', 'rust:1.21.0'
]
def cargo(args, remaining):
sys.exit(run(command + ['cargo'] + remaining or []).returncode)
parser_cargo = subparsers.add_parser('cargo', help='run a cargo command')
parser_cargo.set_defaults(func=cargo)
def sh(args, remaining):
sys.exit(run(command + ['bash']).returncode)
parser_sh = subparsers.add_parser('sh', help='bring up a shell')
parser_sh.set_defaults(func=sh)
def test(args, remaining):
sys.exit(run(command + ['cargo', 'test']).returncode)
parser_test = subparsers.add_parser('test', help='run tests')
parser_test.set_defaults(func=test)
if len(sys.argv) > 1:
args, remaining = parser.parse_known_args()
try:
args.func(args, remaining)
except FileNotFoundError:
sys.exit('Please install Docker.')
else:
parser.print_help()