factor/library/cli.factor

57 lines
1.7 KiB
Factor
Raw Normal View History

2004-07-16 02:26:21 -04:00
! Copyright (C) 2003, 2004 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license.
2005-09-24 15:21:17 -04:00
IN: kernel
USING: errors hashtables io kernel-internals lists namespaces
parser sequences strings ;
2004-07-16 02:26:21 -04:00
2004-07-30 16:22:20 -04:00
: run-user-init ( -- )
#! Run user init file if it exists
"user-init" get [
"~" get "/.factor-rc" append dup exists?
[ try-run-file ] [ drop ] if
] when ;
2004-07-30 16:22:20 -04:00
2005-09-24 15:21:17 -04:00
: set-path ( value seq -- )
unswons over [ nest [ set-path ] bind ] [ nip set ] if ;
2004-12-24 02:52:02 -05:00
2005-12-28 20:25:17 -05:00
: cli-var-param ( name value -- )
swap ":" split >list set-path ;
2004-12-24 02:52:02 -05:00
2005-09-24 15:21:17 -04:00
: cli-bool-param ( name -- ) "no-" ?head not cli-var-param ;
2004-10-09 21:43:14 -04:00
2004-07-30 16:22:20 -04:00
: cli-param ( param -- )
#! Handle a command-line argument starting with '-' by
#! setting that variable to t, or if the argument is
#! prefixed with 'no-', setting the variable to f.
2004-10-09 21:43:14 -04:00
#!
#! Arguments containing = are handled differently; they
#! set the object path.
2005-09-24 15:21:17 -04:00
"=" split1 [ cli-var-param ] [ cli-bool-param ] if* ;
2004-07-30 16:22:20 -04:00
: cli-arg ( argument -- argument )
#! Handle a command-line argument. If the argument was
#! consumed, returns f. Otherwise returns the argument.
#! Parameters that start with + are runtime parameters.
2005-04-19 20:28:01 -04:00
dup empty? [
2005-05-18 16:26:22 -04:00
"-" ?head [ cli-param f ] when
dup [ "+" ?head [ drop f ] when ] when
] unless ;
2004-07-30 16:22:20 -04:00
2004-12-25 02:55:03 -05:00
: cli-args ( -- args ) 10 getenv ;
2006-03-14 21:09:25 -05:00
: default-shell "tty" ;
2005-03-07 22:11:36 -05:00
: default-cli-args
#! Some flags are *on* by default, unless user specifies
#! -no-<flag> CLI switch
"user-init" on
"compile" on
2005-12-06 23:09:51 -05:00
"native-io" on
"null-stdio" off
macosx? "cocoa" set
unix? macosx? not and "x11" set
2006-03-14 21:09:25 -05:00
default-shell "shell" set ;
2005-03-07 22:11:36 -05:00
2004-12-25 02:55:03 -05:00
: parse-command-line ( -- )
cli-args [ cli-arg ] subset [ try-run-file ] each ;