-- James Cook -- created: 7/8/04 -- modified: 7/9/04 -- updated: 4/30/08 -- Command-line Processing Example Program -- copy this template program and use it in your own program -- Description -- Below is the code for a simple program using a command-line processing -- routine, contained in main(). main()'s fundamental job is to process the -- command-line arguments and then call various user-defined sub-routine -- procedures. -- Run the program using: -- ex cmdshell.ex -- exwc cmdshell.ex -- exu cmdshell.ex -- Note: -- This program is based, in structuring-layout, to my shell.cpp program in -- C++. Platform independent: Should work on all platforms of Euphoria. ------------------ -- Include Section include get.e include misc.e include wildcard.e ---------------- -- Local Section constant myprogExe = "ex myprog" -- command-line string needed to run your program constant tab = "\t" -- Sub-routines: procedure display_usage() puts(1, "\n") puts(1, "Use: " & myprogExe & " [options] < infile > outfile\n") puts(1, "\n") puts(1, "[options]\n") puts(1, "\n") puts(1, " /?" &tab&"immediately processes command display_usage() before processing other\n") puts(1, tab&"commands\n") puts(1, "\n") puts(1, " -?" &tab&"processes display_usage() in order\n") puts(1, "\n") puts(1, " -exN" &tab&"puts N (string) to screen\n") puts(1, "\n") puts(1, " -etc."&tab&"puts the following arguments to screen\n") puts(1, "\n") end procedure -- add more sub-routine procedures here. constant cmdo = 2 -- cmdln_arg_offset -- desc. Command-line argument offset, different in C++ function main(sequence cmd) -- command-line processing: integer len_cmd -- len_cmd -- desc. length(cmd) , len_args -- len_arguments -- desc. length(args) , len_arg -- len_argument -- desc. length(arg) , arg_n -- argument_number -- desc. argument number sequence args -- arguments -- desc. command-line arguments , arg -- argument -- desc. command-line argument sequence lwr_arg -- lower_argument -- desc. optional, uncomment only if used integer len_option -- len_option -- desc. optional, uncomment only if used sequence str -- string -- desc. optional, uncomment only if used sequence buffer -- buffer -- desc. optional, uncomment only if used -- cmd structure: -- [1] is .exe path -- [2] is .e path (same as [1] when translated/compiled) -- [3] is first command line argument -- [n] is nth command line argument -- length(cmd) - 2 is the number of command line arguments len_cmd = length(cmd) len_args = len_cmd - cmdo if len_args = 0 then display_usage() return 1 end if args = cmd[cmdo+1..len_cmd] if find("/?",args) then display_usage() return 1 end if arg_n = 0 -- the code is cleaner when starting with 0 instead of 1 while arg_n < len_args do -- option specifier is '-' -- (all 'arg' variables are grouped together) arg_n += 1 arg = args[arg_n] len_arg = length(arg) -- optional, uncommented only if used lwr_arg = lower(arg) -- optional, uncommented only if used if equal(arg,"-?") then -- call sub-routine procedure: display_usage() --return 1 elsif match("-ex",arg) = 1 then len_option = 3 str = arg[len_option+1..len_arg] -- call sub-routine procedure: -- test: puts(2,str & '\n') elsif equal(lwr_arg,"-etc.") then buffer = {} while arg_n < len_args do arg_n += 1 arg = args[arg_n] --lwr_arg = lower(arg) -- optional, uncommented only if used if arg[1] = '-' then arg_n -= 1 exit end if buffer = append(buffer, arg) end while --> do something with 'buffer' -- call sub-routine procedure: -- test: for i=1 to length(buffer) do puts(1, buffer[i] & '\n') end for else -- put your own error message here: puts(2,"ERR: ARGUMENT_SYNTAX_ERROR\n") display_usage() return 2 -- ARGUMENT_SYNTAX_ERROR end if end while return 0 end function -- Start of Execution: constant error_level = main(command_line()) -- comment-out any of the following lines if you do not want this behavior in -- your program if platform() = WIN32 then puts(1, "Press any key to continue . . .") -- emulate Dos' Pause command. if wait_key() then end if end if abort(error_level) -- return the error level to the system -- eof