7715
Example IDL program illustrating an approach to providing command line arguments to a IDL Runtime or IDL Virtual Machine application
;+
; :Description:
;
; Exelis VIS Technical Support example program illustrating how
; one might start an IDL session (development, runtime (RT)
; virtual machine (VM)) with the "-args" command line switch
; to provide command line argument inputs to the IDL session,
; which can then be accessed by a program running in that IDL
; session with the COMMAND_LINE_ARGS function.
;
; This example program can take regular input parameter and
; keyword arguments when called within an IDL session or program.
; However, if any IDL session command line arguments are detected
; then override statement call parameter and keyword inputs from
; above and assume all inputs are provided from the "-args"
; command line switch values for the IDL session.
;
; :Parameters:
;
; MYARG1 : (optional) (float)
; MYARG2 : (optional) (string)
;
; :Keywords:
;
; KW_INT : (integer)
; KW_STR : (string)
;
; :Syntax: (When called in an IDL statement)
;
; mypro [,myarg1 [,myarg2]] [,KW_INT=<integer>] [KW_STR=<string>]
;
; For example:
;
; IDL> mypro, 1.23, "Hello World", KW_INT=99, KW_STR='Dog Cat'
;
; :Command line argument usage: (provided at IDL session startup)
;
; <IDL-command> -args [myarg1 [myarg2]] [KW_INT=<integer>] [KW_STR=<string>]
;
; --where "<IDL-command>" would be the command to start the IDL
; session and where each command line argument to the -args switch
; is provided as a string.
;
; For example, if mypro.pro was compiled and saved into an IDL
; application save file, "mypro.sav" (e.g., .COMPILE...,
; RESOLVE_ALL, SAVE...), it could then be called from an IDL VM
; session like:
;
; On Linux, Mac, Solaris:
;
; idl -vm=mypro.sav -args 1.23 "Hello World" KW_INT=99, "KW_STR=Dog Cat"
;
; On Windows:
;
; idlrt.exe -vm=mypro.sav -args 1.23 "Hello World" KW_INT=99, "KW_STR=Dog Cat"
;
; For program simplicity, please notice that even though the
; KW_STR assignment string contains an embedded space character
; value, that substring is *not* expected to be surrounded by an
; internal set of single quotes like:
;
; "KW_STR='Dog Cat'" <== *wrong*
;
; Output for the example command line argument calls above might
; look like this:
;
; MYARG1 FLOAT = 1.23000
; MYARG2 STRING = 'Hello World'
; KW_INT INT = 99
; KW_STR STRING = 'Cat Dog'
;
; :References:
;
; See the IDL Help section titled, "Command-line Options for IDL
; Startup" for more information about the -args command line
; switch.
;
; See also the IDL Help reference page for the COMMAND_LINE_ARGS
; function.
;
; Documentation Center:
;
; http://www.exelisvis.com/docs/command_line_options_for.html#Command
; http://www.exelisvis.com/docs/COMMAND_LINE_ARGS.html
;
; :Author:
; Exelis VIS Technical Support (ju, 17-Feb-2015)
;-
PRO mypro, myarg1, myarg2, KW_INT=kw_int, KW_STR=kw_str
COMPILE_OPT IDL2
args = COMMAND_LINE_ARGS(COUNT=argc)
; If any IDL session command line arguments are detected then override
; procedure parameter and keyword arguments inputs from above and assume
; inputs are provided from the -args command line switch values for
; the IDL session.
IF argc GT 0 THEN BEGIN
; Process each element of IDL session -args command line arguments
FOREACH arg, args DO BEGIN
; If there is a "=" in argument string then handle it as KW input
; otherwise handle as positional parameter input.
CASE STREGEX( arg, '=', /BOOLEAN ) OF
; Keyword parameter string contains an equal sign character
1 : BEGIN
; Split keyword assignment string into its KW and value parts
kw_parts = STRSPLIT( arg, '=', /EXTRACT)
; Handle KW value assignments
CASE STRUPCASE( STRCOMPRESS( kw_parts[0], /REMOVE_ALL ) ) OF
'KW_INT' : $ ; Integer keyword parameter value
kw_int = FIX( kw_parts[1] )
'KW_STR' : $ ; String keyword parameter value
kw_str = STRTRIM( kw_parts[1], 2 ) ; Remove leading or trailing strings
ELSE : ; Unknown keywords
ENDCASE
END
; Otherwise handled arg as a positional parameter
ELSE : BEGIN
IF ~ISA(myarg1) THEN $ ; myarg1 defined yet?
myarg1 = FLOAT(arg) $
ELSE IF ~ISA(myarg2) THEN $ ; myarg2 defined yet?
myarg2 = arg
END
ENDCASE
ENDFOREACH
ENDIF
; Verify inputs values were handled properly
HELP, myarg1, myarg2, kw_int, kw_str, OUTPUT=output
PRINT, output, FORMAT='(a)'
; If running from an IDL Runtime or IDL VM session then
; display output to a dialog.
IF LMGR( /RUNTIME ) && (!VERSION.OS_FAMILY EQ 'Windows') THEN $
res = DIALOG_MESSAGE(output, /INFORMATION)
END ; mypro
; [ Reviewed for external: JU (17-Feb-2015) ]