diff --git a/cli-v1-to-v2 b/cli-v1-to-v2 index 175afa7..60a19b6 100755 --- a/cli-v1-to-v2 +++ b/cli-v1-to-v2 @@ -20,7 +20,7 @@ _SLICE_TYPE_RE = re.compile( flags=re.DOTALL ) _FLAG_LITERAL_RE = re.compile( - '(?P[^&]?)cli\\.(?P\\w+)Flag{', + '(?P\\s+)cli\\.(?P\\w+)Flag{', ) _COMMAND_SLICE_RE = re.compile( '(?P\\[\\])cli\\.Command{', @@ -28,6 +28,11 @@ _COMMAND_SLICE_RE = re.compile( _COMMAND_LITERAL_RE = re.compile( '(?P\\s+)cli\\.Command{', ) +_EXIT_ERROR_RE = re.compile('cli\\.NewExitError') +_BOOL_T_FLAG_RE = re.compile( + 'cli\\.BoolTFlag{(?P[^}]*)}', + flags=re.DOTALL +) _MIGRATORS = {} @@ -162,6 +167,21 @@ def _migrate_v1_imports(source): return _V1_IMPORT_RE.sub(_V2_IMPORT, source) +@_migrator +def _migrate_new_exit_error(source): + return _EXIT_ERROR_RE.sub('cli.Exit', source) + +@_migrator +def _migrate_bool_t_flag(source): + return _BOOL_T_FLAG_RE.sub(_bool_t_flag_repl, source) + + +def _bool_t_flag_repl(match): + return 'cli.BoolFlag{{Value: true, {}}}'.format( + match.groupdict()['args'] + ) + + def test_migrators(): import difflib @@ -235,6 +255,30 @@ _MIGRATOR_TESTS = ( \t"gopkg.in/urfave/cli.v2" \t"gopkg.in/urfave/cli.v2" \t"gopkg.in/urfave/cli.v2" +"""), + (""" +\t\t\t\treturn cli.NewExitError("foo whatebber", 9) +""", """ +\t\t\t\treturn cli.Exit("foo whatebber", 9) +"""), + (""" +\t\t\tapp.Flags = []cli.Flag{ +\t\t\t\tcli.StringFlag{ +\t\t\t\t\tName: "aha", +\t\t\t\t}, +\t\t\t\tcli.BoolTFlag{ +\t\t\t\t\tName: "blurp", +\t\t\t\t}, +\t\t\t} +""", """ +\t\t\tapp.Flags = []cli.Flag{ +\t\t\t\t&cli.StringFlag{ +\t\t\t\t\tName: "aha", +\t\t\t\t}, +\t\t\t\t&cli.BoolFlag{Value: true, +\t\t\t\t\tName: "blurp", +\t\t\t\t}, +\t\t\t} """) ) diff --git a/runtests b/runtests index 5dffd7d..4975f96 100755 --- a/runtests +++ b/runtests @@ -88,7 +88,8 @@ def _migrations(): _run(['python', migration_script, '-w', gofile]) _run('go build -o tmp.out {}'.format(gofile).split()) finally: - shutil.rmtree(tmpdir, ignore_errors=True) + if os.environ.get('NOCLEAN', '') == '': + shutil.rmtree(tmpdir, ignore_errors=True) def _run(command):