Skip to content

Commit

Permalink
[stdlib] Skeleton for stdlib/{args,testing}.sh
Browse files Browse the repository at this point in the history
- args: Args definition, and parseArgs()
- testing: describe and assert

This brought up a whole bunch of issues
  • Loading branch information
Andy C committed Aug 27, 2023
1 parent e9a1acf commit 09b13a4
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 23 deletions.
44 changes: 26 additions & 18 deletions spec/ysh-builtin-argparse.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,24 @@

#### Argparse bool option and positional

hay define ArgSpec
hay define ArgSpec/Arg
source --builtin args.ysh

ArgSpec myspec {
Arg -v --verbose { type = Bool }
Arg src
Arg dst
Args :spec {
flag -v --verbose (Bool)
arg src
arg dst
}
var args = ['-v', 'src/path', 'dst/path']
argparse (myspec, args, :opts)
var argv = ['-v', 'src/path', 'dst/path']

json write (opts)
json write (args)
# Gah we don't have destructuring assignment?
# Also need to define :spec

var arg = parseArgs(spec, argv)

# var arg, i = parseArgs(spec, argv)

json write (arg)
json write (i)
## STDOUT:
{
"verbose": true,
Expand All @@ -33,21 +38,24 @@ json write (args)
## END

#### Argparse basic help message
hay define ArgSpec
hay define ArgSpec/Arg

ArgSpec myspec {
source --builtin args.ysh

Args :spec {
description = '''
Reference Implementation
'''
prog = "program-name"
Arg -v --verbose { type = Bool; help = "Verbose" }
Arg src
Arg dst

arg -v --verbose (Bool, help = "Verbose")
arg src
arg dst
}
var args = ['-h', 'src', 'dst']
var argv = ['-h', 'src', 'dst']

# Help
var arg = parseArgs(spec, argv)

argparse (myspec, args, :opts)
## STDOUT:
usage: program-name [-h] [-v] src dst

Expand Down
17 changes: 12 additions & 5 deletions spec/ysh-builtin-describe.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@

#### Describe Prototype

hay define argparse

# Oops, we're running into this problem ...

hay define argparse/flag
source --builtin testing.ysh

proc p {
echo STDOUT
Expand All @@ -30,6 +26,17 @@ describe p {

cat out
cat err

# Oh man the here docs are still useful here because of 'diff' interface
# Multiline strings don't quite do it

diff out - <<< '''
STDOUT
'''

diff err - <<< '''
STDERR
'''
}
}

Expand Down
39 changes: 39 additions & 0 deletions stdlib/args.ysh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# args.ysh
#
# Usage:
# source --builtin args.sh
#
# Args :spec {
# flag -v --verbose "Verbosely" # default is Bool, false
# flag -P --max-procs (
# Int, default=-1,
# desc="Run at most P processes at a time"
# )
# arg src (desc="Source")
# arg src (desc="Dest")
# }
#
# var arg, i = parseArgs(spec, ARGV)
#
# echo "Verbose $[arg.verbose]"
#

proc Args(spec Ref ; ; block) {
# TODO: evaluate block somehow
#
# eval ( ) function does it I guess?
#
# You have to put 'flag' and 'arg' in scope

echo hi

#= spec
#= block
}

func parseArgs(spec, argv) {
var i = 0
var arg = {}

return ([arg, i])
}
22 changes: 22 additions & 0 deletions stdlib/testing.ysh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# testing.ysh
#
# Usage:
# source --builtin testing.sh
#
# func f(x) { return (x + 1) }
#
# describe foo {
# assert (43 === f(42))
# }

proc describe (...desc, ) {
echo hi
}

# is 'cond Expr' lazily evaluated?
# Or all proc arguments are autoomatically lazy, except words?

proc assert (; cond ) {
echo hi
}

0 comments on commit 09b13a4

Please sign in to comment.