Pitonyak::CommandLineProcessor - Recognize and parse command line arguments.


After writing the same code to process arguments over and over again, I decided to write a processor that could handle most of the tasks for me.

This processor will process the arguments and place them in an array after matching commands with arguments for me.

The processor also recognizes and parses arguments from text files.


More in depth description??



Return the state of the initial die_on_error flag.


Return the state of the die_on_error flag.


Set the state of the die_on_error flag.



Clear the argument configuration and argument values.



Clear the argument configuration.



Clear the argument values.



Get a reference to all of the accumulated arguments.


$obj->push_processed_arg($arg_name, $arg_value, $method_name)

Push a processed argument set onto the stack.

Argument Description
arg_name Argument name without the leading minus.
arg_value Value
method_nameName of the method to use to parse the argument/value.



Process each argument based on the current configuration. An argument list is built by calling get_processed_args().


$obj->process_args_from_file($file_name, ...)

Process arguments from each file. Empty lines are ignored. The general process is to open and read values from every file and then process the lines.



Configure this object using a configuration file. Typical values are as follows:

Key Description
die_on_error If 1, an error causes program termination.
arg_names Comma separated list of expected argument names.

Each supported argument is assumed to its own entry. A typical examle might be:

arg_names = s, ReadFile
arg = s
arg.${arg}.case_sensitive = 0
arg.${arg}.expects_value = 0
arg.${arg}.allows_value = 1
arg.${arg}.force_value_in_same_arg = 1
arg.${arg}.method_name =
arg.${arg}.description = Recursively traverse directories.
arg.${arg}.arg_is_file_name_to_read = 0

arg = ReadFile
arg.${arg}.case_sensitive = 0
arg.${arg}.expects_value = 1
arg.${arg}.allows_value = 1
arg.${arg}.force_value_in_same_arg = 0
arg.${arg}.method_name =
arg.${arg}.description = Read arguments from the specified text file.
arg.${arg}.arg_is_file_name_to_read = 1

The supported argument configuration items are as follows:

Key Description
case_sensitive Is this argument case sensitive?
expects_value Argument uses a corresponding value.
allows_value Will use a value in the same argument if present.
force_value_in_same_arg Will only accept a value if embedded in the argument using an equal sign.
method_name Method to call to process this argument.
description Text description to print with help.
arg_is_file_name_to_read This argument causes arguments to be read from the specified file.



Make a copy of one object into another

$obj1-copy($obj2)> is the same as $obj1 = $obj2.


The get_class_attribute method utilizes the fact that $obj->method(@parms) is the same as method($obj, @parms). This method does not perform type checking to verify that this is true.

The get_class_attribute method is rarely called directly.


With only one paramter, the first parameter is assumed to be an attribute name and the default attribute value is returned.


With two arguments, the first is assumed to be a ADPLogger object and the second is assumed to be an attribute name. The attribute value for the object is returned.

$obj->get_class_attribute($attribute_value, $attribute_name)

With three arguments, the first is assumed to be the object, the second is a new attribute value, and the third is the attribute name to set. Although the order seems odd, this is intentional.

Consider the method is_ok defined as return get_class_attribute( @_, 'is_ok' );

Remember that @_ refers to the argument list passed to the method. In all cases, the last argument passed to get_class_attribute is the attribute name. If the method is called directly, this is the only argument.


$obj = new Pitonyak::CommandLineProcessor;

Generate a new object.

$obj = $obj->new()

Generate a new copy of an object.



Return 1 if $arg starts with a minus sign.



Obtain the argument name if $arg is preceded by a minus sign. Execpted use is something like:

$arg_name = $obj->get_argument_name($arg) if $obj->is_argument($arg);

If $arg is not an argument, then undef is returned.

The returned name is take from the argument hash, and is not the precise value of the argument. So, if "filename" is not case sensitive, then FileName, and filename will both return filename.



Return 1 if $arg expects an argument, 0, otherwise.



Return 1 if $arg is of the form '-argument=value', otherwise, return 0.



If $arg is of the form '-argument=value', then return value. If it is not, then return undef.

Private Methods



The initialize() method is called automatically when an object is created. The new method also calls initialize() directly

Initialize the data structure by copying values from the initial attributes hash into the newly created object. Finally, set the read properties hash to an empty reference.