8 #ifndef NIDAS_CORE_NIDASAPP_H
9 #define NIDAS_CORE_NIDASAPP_H
23 namespace nidas {
namespace core {
58 if (i < (
int)args.size())
119 const std::string& syntax =
"",
120 const std::string&
usage =
"",
121 const std::string& default_ =
"",
122 bool required =
false);
161 addFlag(
const std::string& flag);
179 args.push_back(
this);
194 usage(
const std::string& indent =
" ");
261 accept(
const std::string& flag);
817 nidas_app_arglist_t::const_iterator it;
818 for (it = arglist.begin(); it != arglist.end(); ++it)
820 (*it)->acceptShortFlag(!require);
927 parseArgs(
int argc, const
char* const argv[])
throw (NidasAppException);
961 parseInputs(
const std::vector<std::string>& inputs,
962 std::string default_input =
"",
1064 getDataset(
const std::string& datasetname)
1139 usage(
const std::string& indent =
" ");
1166 addSignal(
int signum,
void (*callback)(
int signum) = 0,
bool nolog=
false);
1244 std::list<std::string>&
1393 nidas::util::auto_ptr<nidas::util::SocketAddress>
_sockAddr;
1445 const std::vector<std::string>& args) :
1448 vargv.push_back(strdup(argv0.c_str()));
1449 for (
unsigned int i = 0; i < args.size(); ++i)
1451 vargv.push_back(strdup(args[i].c_str()));
1465 return std::vector<std::string>(
vargv.begin()+optindex,
vargv.end());
1470 for (
int i = 0; i <
argc; ++i)
1491 #endif // NIDAS_CORE_NIDASAPP_H
nidas::core::Dataset throw(nidas::util::InvalidParameterException, XMLException)
Derive a path to an XML datasets file according to the current environment settings, searching these paths in order:
Definition: NidasApp.cc:1526
nidas_app_arglist_t getArguments()
Return the list of arguments which are supported by this NidasApp, in other words, the arguments enabled by enableArguments() or requireArguments().
Definition: NidasApp.cc:687
std::string getConfigsXML()
Derive the path to the XML file which lists project configs.
Definition: NidasApp.cc:378
SampleMatcher _sampleMatcher
Definition: NidasApp.h:1385
int _outputFileLength
Definition: NidasApp.h:1396
bool _processData
Definition: NidasApp.h:1379
void enableArguments(const nidas_app_arglist_t &arglist)
Add the list of NidasAppArg pointers to the set of arguments accepted by this NidasApp instance...
Definition: NidasApp.cc:647
float asFloat()
Same as asInt(), except parse the argument value as a float.
Definition: NidasApp.cc:207
std::list< std::string > _dataFileNames
Definition: NidasApp.h:1391
bool _required
Definition: NidasApp.h:297
static NidasApp * getApplicationInstance()
Return the current application instance.
Definition: NidasApp.cc:611
std::string _default
Definition: NidasApp.h:293
std::string getName()
Definition: NidasApp.h:706
void setApplicationInstance()
An instance of NidasApp can be set as an application-wide instance, like an application context...
Definition: NidasApp.cc:603
void requireArguments(const nidas_app_arglist_t &arglist)
Add arguments to this NidasApp same as enableArguments() but also set them as required.
Definition: NidasApp.cc:655
void setProcessData(bool process)
Set the process-data setting in this NidasApp.
Definition: NidasApp.h:1017
A class for parsing, formatting and doing operations on time, based on Unix time conventions, where leap seconds are ignored, so that there are always 60 seconds in a minute, 3600 seconds in an hour and 86400 seconds in a day.
Definition: UTime.h:76
static void setupSignals(void(*callback)(int signum)=0)
Setup signal handling for HUP, INT, and TERM, equivalent to calling addSignal() for each...
Definition: NidasApp.cc:1122
int _argi
Definition: NidasApp.h:1413
NidasAppArg Version
Definition: NidasApp.h:667
std::string _argv0
Definition: NidasApp.h:1377
unsigned int dsm_sample_id_t
Definition: Sample.h:63
NidasAppArgv(const std::string &argv0, const std::vector< std::string > &args)
Definition: NidasApp.h:1444
NidasAppArg SampleRanges
Definition: NidasApp.h:664
uid_t getGroupID()
Return the groupid of the username passed to the Username argument, otherwise 0.
Definition: NidasApp.h:1300
NidasAppArg LogConfig
Definition: NidasApp.h:657
Exception which can be built from an xerces::XMLException, xercesc::SAXException, or xercesc::DOMExce...
Definition: XMLException.h:43
Definition: NidasApp.h:588
NidasAppArg Username
Definition: NidasApp.h:670
std::string _syntax
Definition: NidasApp.h:291
static void setInterrupted(bool interrupted)
Set the global interrupted state for NidasApp.
Definition: NidasApp.cc:1113
nidas_app_arglist_t loggingArgs()
This is a convenience method to return all of the logging-related options in a list, such that this list can be extended if new log options are enabled.
Definition: NidasApp.cc:1162
std::string _xmlFileName
Definition: NidasApp.h:1381
std::string outputFileName()
Return just the output file pattern specified by an output option, without the file length specifier...
Definition: NidasApp.h:985
An interface for a socket address.
Definition: SocketAddress.h:36
void lockMemory()
Attempt mlockall() for this process, first adding the CAP_IPC_LOCK capability if possible.
Definition: NidasApp.cc:1388
uid_t getUserID()
Return the userid of the username passed to the Username argument, otherwise 0.
Definition: NidasApp.h:1290
NidasAppArg DebugDaemon
Definition: NidasApp.h:672
void setFlags(const std::string &flags)
Completely replace the flags which this argument should accept.
Definition: NidasApp.cc:150
bool interrupted(bool allow_exception=true)
Return the value of the global interrupted flag.
Definition: NidasApp.cc:1075
void checkRequiredArguments()
Verify that all NidasAppArg arguments required by this NidasApp have been specified, otherwise throw NidasAppException.
Definition: NidasApp.cc:668
IdFormat getIdFormat()
Definition: NidasApp.h:1185
~NidasApp()
If this instance is the current application instance, then the application instance is reset to null ...
Definition: NidasApp.cc:579
Definition: NidasApp.h:588
std::string _flags
Definition: NidasApp.h:290
void parseLogLevel(const std::string &optarg)
This is an alias for parseLogConfig(), since the LogConfig string syntax is backwards compatible with...
Definition: NidasApp.h:941
nidas::util::UTime _endTime
Definition: NidasApp.h:1389
NidasAppArg PidFile
It is not enough to enable this arg in an app, the app must must call checkPidFile() as well...
Definition: NidasApp.h:680
NidasApp(const std::string &name)
Construct a NidasApp instance and give it a name to be used for the usage info.
Definition: NidasApp.cc:415
void startArgs(const ArgVector &args)
Set the list of command-line argument strings to be parsed and handled by successive calls to nextArg...
Definition: NidasApp.cc:741
A Dataset is a named collection of parameters, that are used in data processing.
Definition: Datasets.h:47
nidas::util::SocketAddress * socketAddress()
If parseInputs() parsed a network socket specifier, then this method returns a pointer to the corresp...
Definition: NidasApp.h:1256
std::string xmlHeaderFile()
Definition: NidasApp.h:1032
const std::string & getFlag()
Return the command-line flag which this argument consumed.
Definition: NidasApp.cc:130
NidasAppArg EndTime
Definition: NidasApp.h:663
NidasAppArg LogParam
Definition: NidasApp.h:659
void parseLogConfig(const std::string &optarg)
Parse a LogConfig from the given argument using the LogConfig string syntax, and add that LogConfig t...
Definition: NidasApp.cc:695
bool allowUnrecognized()
Return whether unrecognized flags are allowed or not.
Definition: NidasApp.cc:1315
int asInt()
Parse the argument value as an integer, where the value could be the default if no value has been exp...
Definition: NidasApp.cc:180
nidas::util::UTime parseTime(const std::string &optarg)
Definition: NidasApp.cc:717
char ** argv
Definition: NidasApp.h:1477
A LogScheme is a vector of LogConfig's and the vector of fields to show in log messages.
Definition: Logger.h:623
Definition: NidasApp.h:588
nidas::util::UTime _startTime
Definition: NidasApp.h:1387
Definition: NidasApp.h:588
void setUsageString(const std::string &text)
Definition: NidasApp.h:264
ArgVector parseArgs(const ArgVector &args)
Parse all arguments accepted by this NidasApp in the command-line argument list args, but only handle the standard arguments.
Definition: NidasApp.cc:916
std::string _value
Definition: NidasApp.h:295
~NidasAppArgv()
Definition: NidasApp.h:1468
int checkPidFile()
Create a pid file for this process and return 0.
Definition: NidasApp.cc:1611
bool _help
Definition: NidasApp.h:1398
int argc
Definition: NidasApp.h:1478
void acceptShortFlag(bool enable)
Set whether short flags are enabled or not.
Definition: NidasApp.h:151
bool single()
Return true for arguments which are only a single argument.
Definition: NidasApp.cc:259
bool _allowUnrecognized
Definition: NidasApp.h:1418
void parseInputs(const std::vector< std::string > &inputs, std::string default_input="", int default_port=0)
Parse one or more input URLs from a list of non-option command-line arguments.
Definition: NidasApp.cc:930
nidas::util::LogScheme _logscheme
NidasApp keeps track of the LogScheme built up from arguments.
Definition: NidasApp.h:1421
nidas::util::UTime getEndTime()
Definition: NidasApp.h:1074
std::string _arg
Definition: NidasApp.h:294
std::string getHostName()
Return the hostname passed to the Hostname argument, if any, otherwise return the current hostname as...
Definition: NidasApp.cc:1575
NidasAppInputFilesArg InputFiles
Definition: NidasApp.h:668
std::vector< NidasAppArg * > nidas_app_arglist_t
Lists of arguments can be manipulated together by putting them into this container type...
Definition: NidasApp.h:41
NidasAppException(const std::string &what)
Definition: NidasApp.h:34
nidas_app_arglist_t _app_arguments
Definition: NidasApp.h:1410
Definition: Exception.h:35
NidasAppArg FormatSampleId
Definition: NidasApp.h:666
bool _deleteProject
Definition: NidasApp.h:1408
std::string _outputFileName
Definition: NidasApp.h:1395
bool isRequired()
Return whether this argument is required.
Definition: NidasApp.cc:102
std::string getUserName()
Return the username passed to the Username argument, if enabled, otherwise return an empty string...
Definition: NidasApp.h:1280
std::string getProcessName()
Get the process name, as set by setProcessName(), or else return the app name from getName()...
Definition: NidasApp.cc:625
NidasAppArg * parseNext()
Parse the next recognized argument from the list set in startArgs().
Definition: NidasApp.cc:777
void setRequired(bool isRequired=true)
Set whether this argument is required.
Definition: NidasApp.cc:94
NidasAppArg ProcessData
Definition: NidasApp.h:661
void setupDaemonLogging()
Call setupDaemonLogging(daemon_mode), where daemon_mode is true only if DebugDaemon is false...
Definition: NidasApp.cc:1332
A NidasAppArg is command-line argument which can be handled by NidasApp.
Definition: NidasApp.h:73
Project * getProject()
Return a pointer to the application-wide Project instance.
Definition: NidasApp.cc:1205
std::string usage(const std::string &indent=" ")
Return a usage string describing the arguments accepted by this application, rendering each argument ...
Definition: NidasApp.cc:1188
NidasAppArg XmlHeaderFile
Definition: NidasApp.h:655
std::string formatId(dsm_sample_id_t sid)
A more convenient form of formatSampleId() which just returns a string, and also includes the DSM id ...
Definition: NidasApp.cc:1286
int logLevel()
Return LogScheme::logLevel() for the current log scheme.
Definition: NidasApp.cc:1297
The NidasApp class throws a NidasAppException when command-line options do not parse.
Definition: NidasApp.h:31
nidas_app_arglist_t operator|(nidas_app_arglist_t arglist1, nidas_app_arglist_t arglist2)
Combine two arglists into a single arglist without any duplicates.
Definition: NidasApp.cc:1172
bool hasException()
See setException().
Definition: NidasApp.cc:1097
bool _enableShortFlag
Definition: NidasApp.h:296
NidasAppArg LogShow
Definition: NidasApp.h:656
NidasAppArg FormatHexId
Definition: NidasApp.h:665
NidasAppArg ConfigsArg
Definition: NidasApp.h:673
ArgVector _argv
Definition: NidasApp.h:1412
IdFormat _idFormat
Definition: NidasApp.h:1383
nidas::util::UTime getStartTime()
Definition: NidasApp.h:1068
NidasAppArg Help
Definition: NidasApp.h:660
std::string _appname
Definition: NidasApp.h:1375
void setDefault(const std::string &dvalue)
Definition: NidasApp.h:277
std::string _usage
Definition: NidasApp.h:292
std::string _username
Definition: NidasApp.h:1400
bool inputsProvided()
Definition: NidasApp.h:968
static void addSignal(int signum, void(*callback)(int signum)=0, bool nolog=false)
Add the given signal signum to the list of signals handled by the NidasApp signal handler...
Definition: NidasApp.cc:1133
NidasAppArg OutputFiles
Definition: NidasApp.h:669
bool _hasException
Definition: NidasApp.h:1415
NidasApp handles common options for NIDAS applications.
Definition: NidasApp.h:572
bool processData()
Return the current process-data setting for this NidasApp.
Definition: NidasApp.h:1026
void setException(const nidas::util::Exception &ex)
Like setInterrupted(true), but also allows an exception to be stored which can be tested and retrieve...
Definition: NidasApp.cc:1087
NidasAppArg(const std::string &flags, const std::string &syntax="", const std::string &usage="", const std::string &default_="", bool required=false)
Construct a NidasAppArg from a list of accepted short and long flags, the syntax for any arguments to...
Definition: NidasApp.cc:71
std::string getShortHostName()
Like getHostName(), but any '.
Definition: NidasApp.cc:1602
std::string _hostname
Definition: NidasApp.h:1402
virtual bool parse(const ArgVector &argv, int *argi=0)
If argv[argi] matches this argument, then set the flag that was found and also the value if this argu...
Definition: NidasApp.cc:235
void requireLongFlag(const nidas_app_arglist_t &arglist, bool require=true)
Call acceptShortFlag(false) on the given list of NidasApp instances, meaning only the long flag will ...
Definition: NidasApp.h:815
static n_u::SerialPort port
Definition: sing.cc:68
bool asBool()
An argument is true if it is a stand-alone flag and was specified in the arguments, or else if the flag value evaluates to true.
Definition: NidasApp.cc:158
gid_t _groupid
Definition: NidasApp.h:1406
Convert vector<string> args to dynamically allocated (argc, argv) pair which will be freed when the i...
Definition: NidasApp.h:1442
NidasAppArg Hostname
Definition: NidasApp.h:671
void parseUsername(const std::string &username)
Definition: NidasApp.cc:1489
virtual const char * what() const
Definition: Exception.h:98
void setupDaemon()
Call setupDaemon(daemon_mode), where daemon_mode is true only if DebugDaemon is false.
Definition: NidasApp.cc:1363
int outputFileLength()
Return the output file length in seconds.
Definition: NidasApp.h:994
Match samples according to DSM and Sample ID ranges, and configure the ranges with criteria in text f...
Definition: SampleMatcher.h:20
void setupLogScheme()
Definition: NidasApp.cc:556
Exception(const std::string &type, const std::string &n, const std::string &m)
Definition: Exception.h:41
NidasAppArg StartTime
Definition: NidasApp.h:662
std::vector< std::string > ArgVector
Convenience typedef for handling the command-line argv as a vector of strings.
Definition: NidasApp.h:53
std::list< std::string > & dataFileNames()
Return the list of filenames parsed from the input URLs with parseInputs().
Definition: NidasApp.h:1245
void setProcessName(const std::string &argv0)
Set the name of this particular process, usually argv[0].
Definition: NidasApp.cc:618
uid_t _userid
Definition: NidasApp.h:1404
std::string getUsageFlags()
Return the string of flags accepted by this NidasAppArg according to the acceptShortFlag() setting...
Definition: NidasApp.cc:309
NidasAppArgv & operator=(const NidasAppArgv &)
void setupProcess()
Setup a process with the user and group specified by the Username argument, and attempt to set relate...
Definition: NidasApp.cc:1412
nidas::util::auto_ptr< nidas::util::SocketAddress > _sockAddr
Definition: NidasApp.h:1393
void addFlag(const std::string &flag)
Add a flag which this argument should accept.
Definition: NidasApp.cc:138
void parseOutput(const std::string &optarg)
Parse an output specifier in the form <strptime-filename-pattern>[<length>[smh]]. ...
Definition: NidasApp.cc:998
ArgVector unparsedArgs(int optindex)
Given the opt index after getopt() finishes, return a vector of any remaining arguments, suitable for passing to NidasApp::parseInputs().
Definition: NidasApp.h:1463
nidas::util::Exception getException()
Return the current exception.
Definition: NidasApp.cc:1105
const std::string & getValue()
If this argument has been parsed from a command line list (specified() returns true), then return the value passed after the flag.
Definition: NidasApp.cc:118
id_format_t
The four possible output formats for sensor-plus-sample IDs:
Definition: NidasApp.h:587
std::string getDefault()
Definition: NidasApp.h:283
static std::ostream & formatSampleId(std::ostream &out, IdFormat idfmt, dsm_sample_id_t sid)
Write the sensor-plus-sample ID part of sid to stream out using the format specified in idfmt...
Definition: NidasApp.cc:1246
NidasAppArg LogFields
Definition: NidasApp.h:658
void setIdFormat(IdFormat idt)
Store the format in which sample IDs should be shown.
Definition: NidasApp.cc:1238
ArgVector unparsedArgs()
Definition: NidasApp.cc:733
std::string expectArg(const ArgVector &args, int i)
Definition: NidasApp.h:56
NidasAppArg & operator=(const NidasAppArg &)
Definition: NidasApp.h:588
void resetLogging()
Reset logging to the NidasApp default.
Definition: NidasApp.cc:1323
bool nextArg(std::string &arg)
If the next argument to be parsed does not start with '-', then copy it into arg and remove it from t...
Definition: NidasApp.cc:762
NidasAppArg DatasetName
Definition: NidasApp.h:674
SampleMatcher & sampleMatcher()
Use this method to access the SampleMatcher instance for this NidasApp.
Definition: NidasApp.h:1234
bool accept(const std::string &flag)
Return true if the given command-line flag matches one of this argument's flags.
Definition: NidasApp.cc:267
bool helpRequested()
Return true when a help option was parsed, meaning the usage info has been requested.
Definition: NidasApp.h:1173
std::vector< char * > vargv
Definition: NidasApp.h:1476
static NidasApp * application_instance
Definition: NidasApp.h:1373
Definition: InvalidParameterException.h:35
nidas::util::Exception _exception
Definition: NidasApp.h:1416
virtual ~NidasAppArg()
Definition: NidasApp.cc:88
bool specified()
Return true if this argument has been filled in from a command-line argument list, such as after a call to NidasApp::parseArgs().
Definition: NidasApp.cc:110
std::string usage(const std::string &indent=" ")
Render the usage string for this particular argument, taking into account which flags are enabled...
Definition: NidasApp.cc:342