Skip to content

Commit b0dbb88

Browse files
author
Andy C
committed
[builtin/use] Implement use --extern
Add spec tests Start documenting the 'use' command
1 parent b628402 commit b0dbb88

File tree

7 files changed

+82
-42
lines changed

7 files changed

+82
-42
lines changed

builtin/func_reflect.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class Id(vm._Callable):
4040
4141
I guess only mutable objects can have IDs then
4242
"""
43+
4344
def __init__(self):
4445
# type: () -> None
4546
vm._Callable.__init__(self)

builtin/meta_oils.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,13 @@ def _Use(self, cmd_val):
368368
import $LIB_YSH/stdlib
369369
370370
"""
371-
_, arg_r = flag_util.ParseCmdVal('use', cmd_val)
371+
attrs, arg_r = flag_util.ParseCmdVal('use', cmd_val)
372+
arg = arg_types.use(attrs.attrs)
373+
374+
# Accepts any args
375+
if arg.extern_: # use --extern grep # no-op for static analysis
376+
return 0
377+
372378
path_arg, path_loc = arg_r.ReadRequired2('requires a module path')
373379
# TODO on usage:
374380
# - typed arg is value.Place

builtin/pure_ysh.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,9 @@ def Run(self, cmd_val):
206206
# type: (cmd_value.Argv) -> int
207207

208208
# This means we ignore -- , which is consistent
209-
arg, arg_r = flag_util.ParseCmdVal('append',
210-
cmd_val,
211-
accept_typed_args=True)
209+
_, arg_r = flag_util.ParseCmdVal('append',
210+
cmd_val,
211+
accept_typed_args=True)
212212

213213
rd = typed_args.ReaderForProc(cmd_val)
214214
val = rd.PosValue()

doc/ref/chap-builtin-cmd.md

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -353,13 +353,22 @@ Use it like this:
353353

354354
### use
355355

356-
TODO
356+
Import code from other files, creating an `Obj` that acts like a namespace.
357357

358-
Reuse code from other files, respecting namespaces.
358+
use my-dir/my-module.ysh
359359

360-
use lib/foo.ysh # foo myproc, $[foo.attr]
361-
# implicit $_this_dir aka relative import
360+
echo $[my_module.my_integer] # the module Obj has attributes
361+
my_module myproc # the module Obj is invokable
362362

363+
The evaluation of such files is cached, so it won't be re-evaluated if `use` is called again.
364+
365+
<!--
366+
# TODO: implicit $_this_dir aka relative import?
367+
368+
That makes scripts callable from elsewhere?
369+
-->
370+
371+
<!--
363372
Bind a specific name:
364373
365374
use lib/foo.ysh (&myvar) # makes 'myvar' available
@@ -376,10 +385,12 @@ Maybe:
376385
pick log (&mylog)
377386
pick die (&mydie)
378387
}
388+
-->
379389

380-
Also a declaration
390+
The `--extern` flag make the invocation do nothing. It can be used be tools to
391+
analyze what names are in the file.
381392

382-
use --extern grep sed
393+
use --extern grep sed awk
383394

384395
## I/O
385396

doc/ref/toc-ysh.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,13 @@ X [Wok] _field()
116116
shvar Temporary modify global settings
117117
ctx Share and update a temporary "context"
118118
push-registers Save registers like $?, PIPESTATUS
119-
[Modules] runproc Run a proc; use as main entry point
119+
[Introspection] runproc Run a proc; use as main entry point
120+
X extern Run an external command, with an ENV
121+
X invoke Control which "invokables" are run
122+
[Modules]
120123
source-guard guard against duplicate 'source'
121124
is-main false when sourcing a file
122-
X use use names,
125+
use create a module Obj from a source file
123126
[I/O] ysh-read flags --all, -0
124127
ysh-echo no -e -n with simple_echo
125128
write Like echo, with --, --sep, --end

frontend/args.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ def Set(self, name, val):
105105

106106
# debug-completion -> debug_completion
107107
name = name.replace('-', '_')
108+
109+
# similar hack to avoid C++ keyword in frontend/flag_gen.py
110+
if name == 'extern':
111+
name = 'extern_'
112+
108113
self.attrs[name] = val
109114

110115
if 0:

spec/ysh-builtin-module.test.sh

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,50 @@ stdin
3838
status=0
3939
## END
4040

41+
#### use builtin usage
42+
43+
use
44+
echo no-arg=$?
45+
46+
use foo
47+
echo one-arg=$?
48+
49+
use --extern foo
50+
echo extern=$?
51+
52+
use --bad-flag
53+
echo bad-flag=$?
54+
55+
use too many
56+
echo too-many=$?
57+
58+
use ///no-builtin
59+
echo no-builtin=$?
60+
61+
62+
## STDOUT:
63+
no-arg=2
64+
one-arg=1
65+
extern=0
66+
bad-flag=2
67+
too-many=2
68+
no-builtin=1
69+
## END
70+
71+
72+
#### use --extern is a no-op, for static analysis
73+
74+
use --extern grep sed awk
75+
echo status=$?
76+
77+
use --extern zzz
78+
echo status=$?
79+
80+
## STDOUT:
81+
status=0
82+
status=0
83+
## END
84+
4185
#### use foo.ysh creates a value.Obj, and it's cached on later invocations
4286

4387
shopt --set ysh:upgrade
@@ -97,33 +141,3 @@ util die 'hello'
97141
## STDOUT:
98142
## END
99143

100-
#### use builtin usage
101-
102-
use
103-
echo no-arg=$?
104-
105-
use foo
106-
echo one-arg=$?
107-
108-
use --extern foo
109-
echo extern=$?
110-
111-
use --bad-flag
112-
echo bad-flag=$?
113-
114-
use too many
115-
echo too-many=$?
116-
117-
use ///no-builtin
118-
echo no-builtin=$?
119-
120-
121-
## STDOUT:
122-
no-arg=2
123-
one-arg=1
124-
extern=1
125-
bad-flag=2
126-
too-many=2
127-
no-builtin=1
128-
## END
129-

0 commit comments

Comments
 (0)