slic/src
diff -u -r1.1 -r1.2
--- CommandLineProcessor.cc 30 Nov 2006 02:32:27 -0000 1.1
+++ CommandLineProcessor.cc 1 Dec 2006 23:15:07 -0000 1.2
@@ -1,4 +1,4 @@
-// $Id: CommandLineProcessor.cc,v 1.1 2006/11/30 02:32:27 jeremy Exp $
+// $Id: CommandLineProcessor.cc,v 1.2 2006/12/01 23:15:07 jeremy Exp $
#include "CommandLineProcessor.hh"
@@ -24,6 +24,7 @@
m_version_flag(0)
{
registerOptions();
+ createOptionsString();
}
CommandLineProcessor::~CommandLineProcessor()
@@ -52,16 +53,21 @@
{"dump-gdml", optional_argument, 0, 'G'},
{0, 0, 0, 0}
};
-
+
+ static char* optstr = m_getoptOptions.c_str();
+
while (1) {
int option_index = 0;
int c;
-
- // Call getopt to set the next option.
- c = getopt_long (argc, argv, "h?nvm:g:i:o:p:Oxr:s:l:L:d:G",
- long_options, &option_index);
+ // Call getopt for next option.
+ c = getopt_long (argc,
+ argv,
+ optstr,
+ long_options,
+ &option_index);
+
// Done.
if ( c == -1 )
break;
@@ -103,13 +109,11 @@
}
}
- // Version takes first precedence.
// Print the version information and abort.
if (m_version_flag) {
printVersion(std::cout);
abort();
}
- // Help/usage is second precedence.
// Print the usage and abort if the help flag was
// passed or if there were no commandline arguments.
else if (m_help_flag || m_commandline.size() == 0 ) {
@@ -164,11 +168,19 @@
{
// Print application usage.
addOption(new CommandLineOption("h",
- "help",
- "Print SLIC usage.",
- 0,
- 0,
- "/slic/usage"));
+ "help",
+ "Print SLIC usage.",
+ 0,
+ 0,
+ "/slic/usage"));
+
+ // Usage is also activated with the '?' switch.
+ addOption(new CommandLineOption("?",
+ "help",
+ "Print SLIC usage.",
+ 0,
+ 0,
+ "/slic/usage"));
// Run in interactive mode.
addOption(new CommandLineOption("n",
@@ -470,4 +482,18 @@
}
return haveoption;
}
+
+ void CommandLineProcessor::createOptionsString()
+ {
+ m_getoptOptions="";
+ for ( OptionsList::iterator it = m_cmds.begin();
+ it != m_cmds.end();
+ it++ ) {
+ CommandLineOption* opt = *it;
+ m_getoptOptions += opt->getShortName();
+ if ( opt->getMinArgs() ) {
+ m_getoptOptions += ":";
+ }
+ }
+ }
} // namespace slic
slic/include
diff -u -r1.1 -r1.2
--- CommandLineProcessor.hh 30 Nov 2006 02:32:23 -0000 1.1
+++ CommandLineProcessor.hh 1 Dec 2006 23:15:07 -0000 1.2
@@ -33,8 +33,19 @@
public:
+ /**
+ * OptionsPair is a short option name and its associated arguments.
+ */
typedef std::pair<std::string, std::string> OptionsPair;
+
+ /**
+ * CommandLineArguments is the list of options and arguments from the command line.
+ */
typedef std::vector<CommandLineProcessor::OptionsPair> CommandLineArguments;
+
+ /**
+ * OptionsList is a list of CommandLineOption classes describing valid command line switches.
+ */
typedef std::vector<CommandLineOption*> OptionsList;
public:
@@ -52,11 +63,11 @@
* option name with its arguments (if any). Options that can
* occur more than once, such as the '-m' command, get one entry
* per occurrence in the CommandLineArguments. getopt will
- * check that the option is valid valid and that enough arguments
- * were received before the command is put into CommandLineArguments.
- * After calling this function, the function getCommandQueue() will
- * return the list of Geant4 macro commands that resulted from
- * processing the raw command line arguments.
+ * check that the option is valid and that enough arguments
+ * were received, before the command is put into CommandLineArguments.
+ * After calling process(), the getCommandQueue() function can
+ * be used to retrieve the list of Geant4 macro commands that
+ * resulted from processing the raw command line arguments.
*/
void process(int argc, char** argv);
@@ -127,12 +138,18 @@
*/
void processOptions();
+ /**
+ * Automatically create the getopt options string from OptionsList
+ * and set the m_getoptOptions variable to this value.
+ */
+ void createOptionsString();
+
private:
// A vector of options with their arguments.
CommandLineArguments m_commandline;
- // A list Geant4 macro commands filled by processing the command line arguments.
+ // A list of Geant4 macro commands filled by processing the command line arguments.
CommandQueue m_g4q;
// The list of valid options and their associated meta-data.
@@ -142,6 +159,8 @@
int m_help_flag;
int m_interactive_flag;
int m_version_flag;
+
+ std::string m_getoptOptions;
};
}