178 lines
5.4 KiB
C++
178 lines
5.4 KiB
C++
//----------------------------------------------------------------------------
|
|
//
|
|
// Copyright (C) 2004 Intel Corporation
|
|
//
|
|
// File: CmdLineArguments.h
|
|
//
|
|
// Notes: This file contains the definition of a generic class for
|
|
// command line arguments parsing.
|
|
//
|
|
//----------------------------------------------------------------------------
|
|
|
|
#ifndef _COMMAND_LINE_ARGUMENTS_H
|
|
#define _COMMAND_LINE_ARGUMENTS_H
|
|
|
|
#include <map>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
using namespace std;
|
|
|
|
enum color
|
|
{
|
|
BLUE=1,
|
|
GREEN,
|
|
CYAN,
|
|
RED,
|
|
PURPLE,
|
|
MAGENTA,
|
|
LGRAY,
|
|
DGRAY,
|
|
HBLUE,
|
|
HGREEN,
|
|
HCYAN,
|
|
HRED,
|
|
HPINK,
|
|
HYELLOW,
|
|
HWHITE
|
|
};
|
|
|
|
|
|
/*
|
|
* Constants for the common use
|
|
*/
|
|
static const char * CMD_OPT = "opt";
|
|
static const char * CMD_TLS = "tls";
|
|
static const char * CMD_HOST = "host";
|
|
static const char * CMD_VERBOSE = "verbose";
|
|
static const char * CMD_USER = "user";
|
|
static const char * CMD_PASS = "pass";
|
|
static const char * CMD_CERT_NAME = "certName";
|
|
static const char * CMD_PROXY = "proxy";
|
|
static const char * CMD_PROXY_USERNAME = "proxyUser";
|
|
static const char * CMD_PROXY_PASS = "proxyPass";
|
|
static const char* CMD_ACCEPT_SELF_CIGNED_CERT = "acceptSelfSignedCert";
|
|
#if defined (_WIN32) || defined (_WIN64)
|
|
static const char * CMD_LOCAL = "local"; // Only for GeneralInfo and EventLogReader samples
|
|
static const char * CMD_KRB = "krb";
|
|
#else
|
|
static const char * CMD_CERT_PASS = "-certPass";
|
|
#endif
|
|
|
|
static const int AMT_SECURE_PORT = 16993;
|
|
static const int AMT_UNSECURE_PORT = 16992;
|
|
|
|
static const int MAX_USER_LENGTH = 32;
|
|
static const int MAX_PWD_LENGTH = 32;
|
|
static const int MIN_PWD_LENGTH = 8;
|
|
|
|
// Thrown by the CmdLineArguments during parsing of the command line
|
|
class CmdLineException : public exception
|
|
{
|
|
public:
|
|
// Default Constructor
|
|
CmdLineException(string msg) : exception(msg.c_str()) { }
|
|
};
|
|
|
|
// An argument in the command line arguments list.
|
|
struct FormatArgument
|
|
{
|
|
string name; //Name of the argument
|
|
bool hasValue; //Indication whether this argument has a value
|
|
bool mandatory; //Indication whether this argument is mandatory
|
|
bool option; //Indication whether this argument is an option argument: <opt>
|
|
string description; //Description of the argument
|
|
};
|
|
|
|
/*
|
|
CmdLineArguments is a generic class for parsing command line arguments.
|
|
The command line is expected to be of the form: progname.exe -opt1 val1 -opt2 val2...
|
|
where opt1 is the option name, and val1 is the value for this option.
|
|
The option names are specified in advance, and the ones specified are the only valid ones:
|
|
any other option name is considered an error.
|
|
*/
|
|
class CmdLineArguments
|
|
{
|
|
public:
|
|
// Class that represents the entire format of the Command Line Arguments.
|
|
class Format
|
|
{
|
|
public:
|
|
// Default Constructor
|
|
Format():m_formatArguments(){}
|
|
|
|
// Set the command line text color
|
|
void CmdLineArguments::Format::SetConsoleTextColor(color changedColor);
|
|
|
|
// Returns true it two strings are same (without case sensitive).
|
|
bool CompareStringCaseSensitive(const string& str1, const string& str2);
|
|
|
|
// Adds an argument to the format.
|
|
void AddArg(const string name, bool hasValue, bool mandatory, bool option, const string description);
|
|
|
|
// Returns true iff an argument with the given option name exists in the format.
|
|
// If so, set arg with the argument details
|
|
bool GetArg(const string name, FormatArgument & arg) const;
|
|
|
|
// Returns a usage string according to the added arguments
|
|
string CreateUsage(const string & applicationName) const;
|
|
|
|
// Returns a usage string according to the added arguments and the additional data
|
|
string CreateUsage(const string & assemblyName, const string & additionalData) const;
|
|
|
|
// Returns all mandatory arguments.
|
|
vector<FormatArgument> GetMandatoryArgs() const;
|
|
|
|
private:
|
|
// Vector that holds the format's arguments.
|
|
vector<FormatArgument> m_formatArguments;
|
|
|
|
/// Returns a string composed of 3 usage examples according to the given option.
|
|
string CreateExamples(const string & applicationName, const string & argOption) const;
|
|
};
|
|
|
|
// Default ctor. parse() should be used when using it.
|
|
CmdLineArguments():m_cmdLineArguments(){};
|
|
|
|
// Return the value of the argument with the given option.
|
|
// Returns "" if the argument has no value or it doesn't exist, .
|
|
const char* GetArgValue(const string name) const;
|
|
|
|
// Returns true iff an argument with the given option name exists in the CmdLineArguments object.
|
|
bool ArgExists(const string name) const;
|
|
|
|
// Returns the selected option's name
|
|
const char* GetSelectedOptionName() const {return selectedOption.formatArgument.name.c_str();}
|
|
|
|
// Parse a command line according to Format object, and store results in this CmdLineArguments object.
|
|
void Parse(int argc, const char* const argv[], const Format &format, bool hasOptions=true);
|
|
|
|
//Validate IP Address.
|
|
void validateIP(const char* host);
|
|
|
|
//Validate Port.
|
|
void validatePort(const int port);
|
|
|
|
//Validate user name
|
|
void validateUserName(const char* user);
|
|
|
|
//Validate Password
|
|
void validatePassword(const char* password);
|
|
|
|
//Validate Certificate common name
|
|
void validateCertificateCN(const char* commonName);
|
|
|
|
private:
|
|
// An argument in the command line arguments list.
|
|
struct CmdLineArgument
|
|
{
|
|
FormatArgument formatArgument;
|
|
string value;
|
|
};
|
|
// Vector that holds the command line's arguments.
|
|
vector<CmdLineArgument> m_cmdLineArguments;
|
|
CmdLineArgument selectedOption;
|
|
};
|
|
|
|
#endif //#ifndef _COMMAND_LINE_ARGUMENTS_H
|