Skip to content

Commit

Permalink
command-line: adding command-line-options for easy options parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjbq7 committed May 13, 2024
1 parent bfcfc7e commit 4b1204b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
7 changes: 6 additions & 1 deletion basis/command-line/command-line-docs.factor
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
USING: help.markup help.syntax io.pathnames strings system vocabs vocabs.loader ;
USING: help.markup help.syntax io.pathnames sequences strings system vocabs vocabs.loader ;
IN: command-line

HELP: run-bootstrap-init
Expand Down Expand Up @@ -30,6 +30,11 @@ HELP: command-line
{ $var-description "When Factor is run with a script, this variable contains the list of command line arguments which follow the name of the script on the command line. In deployed applications, it contains the full list of command line arguments. In all other cases it is set to " { $link f } "." }
{ $see-also vm-path } ;

HELP: command-line-options
{ $values { "args" sequence } { "args'" sequence } }
{ $description "Used to pop off command-line arguments specified by a '" { $snippet "-" } "' (dash) prefix, setting them as string variables with optional values." } ;


HELP: main-vocab-hook
{ $var-description "Global variable holding a quotation which outputs a vocabulary name. UI backends set this so that the UI can automatically start if the prerequisites are met (for example, " { $snippet "$DISPLAY" } " being set on X11)." } ;

Expand Down
6 changes: 6 additions & 0 deletions basis/command-line/command-line-tests.factor
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,9 @@ USING: command-line namespaces tools.test ;
{ "a:b:c" } [ { "factor" "-roots=a:b:c" } parse-command-line
"roots" get-global
] unit-test

{ { "arg1" "arg2" } t "12" f } [
{ "-foo" "-bar=12" "-no-baz" "arg1" "arg2" }
command-line-options
"foo" get "bar" get "baz" get
] unit-test
5 changes: 5 additions & 0 deletions basis/command-line/command-line.factor
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ SYMBOL: command-line
: param ( param -- )
"=" split1 [ var-param ] [ bool-param ] if* ;

: command-line-options ( args -- args' )
[ dup ?first "-" ?head ] [
[ CHAR: - = ] trim-head param rest
] while drop ;

: (parse-command-line) ( args -- )
[
unclip "-" ?head [
Expand Down

0 comments on commit 4b1204b

Please sign in to comment.