From 3639caf1a8dc0785f3beaf5f154c289ed58f2a09 Mon Sep 17 00:00:00 2001 From: Samuel Hierholzer Date: Tue, 1 Nov 2022 16:36:48 +0100 Subject: [PATCH] [spec/oil] Create two basic spec tests for argparse builtin (#1395) Part of #469 --- spec/oil-builtin-argparse.test.sh | 68 +++++++++++++++++++++---------- 1 file changed, 47 insertions(+), 21 deletions(-) diff --git a/spec/oil-builtin-argparse.test.sh b/spec/oil-builtin-argparse.test.sh index c514a559e2..e0e6ebd62c 100644 --- a/spec/oil-builtin-argparse.test.sh +++ b/spec/oil-builtin-argparse.test.sh @@ -2,34 +2,60 @@ # # Some thoughts before writing code. Hm can we do this entirely in user code, not as a builtin? # -# I think it modifies OPT +# The following is as close as possible to the python argparse which seems to work well +#### Argparse boolsche option and positional +hay define ArgSpec +hay define ArgSpec/Arg -#### Argparse Prototype - -hay define argparse +ArgSpec myspec { + Arg -v --verbose { type = Bool } + Arg src + Arg dst +} +var args = ['-v', 'src/path', 'dst/path'] +argparse (myspec, args, :opts) -# Oops, we're running into this problem ... +json write (opts) +json write (args) +## STDOUT: +{ + "verbose": true, + "src": "src/path", + "dst": "dst/path" +} +# TODO: Should this be empty afterwards? Is it even possible with above call? +[ -hay define argparse/flag +] +## END -# This means we need expr.Type objects? +#### Argparse basic help message +hay define ArgSpec +hay define ArgSpec/Arg + +ArgSpec myspec { + description = ''' + Reference Implementation + ''' + prog = "program-name" + Arg -v --verbose { type = Bool; help = "Verbose" } + Arg src + Arg dst +} +var args = ['-h', 'src', 'dst'] -argparse foo { - flag -v --verbose (Bool) { - help = 'fo' - default = true - } +argparse (myspec, args, :opts) +## STDOUT: +usage: program-name [-h] [-v] src dst - flag -h --help (Bool) { - help = 'fo' - } +Reference Implementation - arg name (pos = 1) { - foo - } -} +positional arguments: + src + dst -## STDOUT: -TODO +options: + -h, --help show this help message and exit + -v, --verbose Verbose ## END