Documentation Set for Swarm 2.2
Prev | Reference I. Defobj Library | Next

# Arguments

## Name

Arguments -- A class that provides customizable command line argument parsing support

## Description

A class that provides customizable command line argument parsing support

## Protocols adopted by Arguments

Create Drop CREATABLE

## Methods

### Phase: Creating

- - (int)parseKey: (int)key arg: (const char *)arg
  This method is called for each option that occurs.

- + createArgc: (int)argc Argv: (const char **)argv appName: (const char *)appName version: (const char *)version bugAddress: (const char *)bugAddress options: (struct argp_option *)options optionFunc: (int (*) (int, const char *))optionFunc inhibitExecutableSearchFlag: (BOOL)inhibitExecutableSearchFlag

- - (void)addOption: (const char *)name key: (int)key arg: (const char *)arg flags: (int)flags doc: (const char *)doc group: (int)group
  Takes an option specification that includes the following information:
  - The name of the option specification
  - The key of the option. This an integer that, if printiable, is the single-character use of the option. For example, `-p' vs. `--protocol' are the different versions of the same thing. One is intended to be mnemonic, the other convenient.
  - If non-NULL, an argument label that says that the option requires an argument (in this case, the protocol name).
  - Flags that change the visibility and parsing of the option
  - Documentation for the option
  - A sorting integer; relative placement of the option in the help screen.

- - addOptions: (struct argp_option *)options

- - setVersion: (const char *)version

- - setBugAddress: (const char *)bugAddress

- - setOptionFunc: (int (*) (int, const char *))optionFunc

- - setAppModeString: (const char *)appModeString

- - setAppName: (const char *)appName

- - setArgc: (unsigned)count Argv: (const char **)theArgv

### Phase: Setting

- - setFixedSeed: (unsigned)seed

- - setDefaultAppDataPath: (const char *)path
  Specify a default path to use for data files when installed location of Swarm cannot be determined. Defaults to current directory.

- - setDefaultAppConfigPath: (const char *)path
  Specify a default path to use for configuration files when installed location of Swarm cannot be determined. Defaults to current directory.

- - setVerboseFlag: (BOOL)verboseFlag

- - setVarySeedFlag: (BOOL)varySeedFlag

- - setBatchModeFlag: (BOOL)batchModeFlag

- - setInhibitExecutableSearchFlag: (BOOL)theInhibitExecutableSearchFlag

- - setInhibitArchiverLoadFlag: (BOOL)inhibitArchiverLoadFlag

### Phase: Using

- - (BOOL)getInhibitArchiverLoadFlag

- - (BOOL)getShowCurrentTimeFlag

- - (const char *)getAppConfigPath
  A path where application-specific configuration files can be expected to be found.

- - (const char *)getAppDataPath
  A path where application-specific data files can be expected to be found.

- - (const char *)getDataPath

- - (const char *)getConfigPath

- - (const char *)getSwarmHome

- - (const char *)getExecutablePath

- - (const char **)getArgv

- - (int)getLastArgIndex

- - (int)getArgc

- - (const char *)getAppModeString

- - (const char *)getAppName

- - (BOOL)getVerboseFlag

- - (unsigned)getFixedSeed

- - (BOOL)getFixedSeedFlag

- - (BOOL)getVarySeedFlag

- - (BOOL)getBatchModeFlag

## Examples

Example defobj/Arguments/1.

     Let's say you want to add a new argument, say `protocol' to your standard
     list of commands.  In other words you want the following to happen at the
     command line when you type --help.
     ------------------------
     mgd@wijiji[/opt/src/mgd/src/mySwarmApp] $ ./mySwarmApp --help
     Usage: mySwarmApp [OPTION...]

       -s, --varyseed             Select random number seed from current time
       -S, --seed=INTEGER         Specify seed for random numbers
       -b, --batch                Run in batch mode
       -m, --mode=MODE            Specify mode of use (for archiving)
       -p, --protocol=PROTOCOL    Set protocol
       -?, --help                 Give this help list
           --usage                Give a short usage message
       -V, --version              Print program version

     Mandatory or optional arguments to long options are also mandatory or
     optional for any corresponding short options.

     Report bugs to bug-swarm@swarm.org.
     -----------------------

     To implement this you need to make your own subclass of Arguments
     like the following:

     #import <defobj/Arguments.h>

     @interface MySwarmAppArguments: Arguments_c
     {
       const char *protocolArg;
     }
     - (const char *)getProtocolArg;
     @end

     @implementation MySwarmAppArguments

     + createBegin: (id <Zone>)aZone
     {
       static struct argp_option options[] = {
         {"protocol", 'p', "PROTOCOL", 0, "Set protocol", 3},
         { 0 }
       };

       MySwarmAppArguments *obj = [super createBegin: aZone];

       [obj addOptions: options];
       return obj;
     }

     - (int)parseKey: (int)key arg: (const char *)arg
     {
       if (key == 'p')
         {
           protocolArg = arg;
           return 0;
         }
       else
         return [super parseKey: key arg: arg];
     }

     - (const char *)getProtocolArg
     {
       return protocolArg;
     }

     @end

     To actually invoke this in the main.m program, you do the following:

     int
     main (int argc, const char ** argv)
     {
       initSwarmArguments (argc, argv, [MySwarmAppArguments class]);

       // the usual - buildObjects:, - buildActions:, - activateIn: calls

       return 0;
     }

Prev | Home | Next
Archiver | Up | BehaviorPhase