Example: Command line arguments

tags:
Contents

Description

Retrieve the list of command-line arguments given to the program. Example command line:

myprogram -c "alpha beta" -h "gamma"

Notes

This class inherits functionality for dealing with command line arguments from class ARGUMENTS. It uses the feature separate_character_option_value to return the values by option name for each of the two arguments. ARGUMENTS provides a rich set of features for command line argument processing.

The simple version in Solution below is as submitted to Rosetta Code to illustrate class ARGUMENTS, but it should be noted that separate_character_option_value is of a detached type and will return a void reference if no value is found for a specified character option. Therefore, a safer version of the use of separate_character_option_value would include object test on the result:

            if attached separate_character_option_value ('c') as l_val then
                print ("Command line argument value for option 'c' is: ")
                print (l_val + "%N")
            end
            if attached separate_character_option_value ('h') as l_val then
                print ("Command line argument value for option 'h' is: ")
                print (l_val + "%N")
            end

Source

Problem description from Rosetta Code

Solution

class
    APPLICATION
inherit
    ARGUMENTS
create
    make
feature {NONE} -- Initialization
    make
            -- Print values for arguments with options 'c' and 'h'.
        do
            print ("Command line argument value for option 'c' is: ")
            print (separate_character_option_value ('c') + "%N")
            print ("Command line argument value for option 'h' is: ")
            print (separate_character_option_value ('h') + "%N")
            io.read_line    -- Keep console window open
        end
end


Output (for command line arguments: -c "alpha beta" -h "gamma")

Command line argument value for option 'c' is: alpha beta
Command line argument value for option 'h' is: gamma