📄 ncbiargs.cpp
字号:
void CArgDesc::VerifyDefault(void) const{ return;}void CArgDesc::SetConstraint(CArgAllow* constraint){ NCBI_THROW(CArgException, eConstraint, s_ArgExptMsg(GetName(), "No-value arguments may not be constrained", constraint ? constraint->GetUsage() : kEmptyStr));}const CArgAllow* CArgDesc::GetConstraint(void) const{ return 0;}string CArgDesc::GetUsageConstraint(void) const{ const CArgAllow* constraint = GetConstraint(); return constraint ? constraint->GetUsage() : kEmptyStr;}///////////////////////////////////////////////////////// Overload the comparison operator -- to handle "AutoPtr<CArgDesc>" elements// in "CArgs::m_Args" stored as "set< AutoPtr<CArgDesc> >"//inline bool operator< (const AutoPtr<CArgDesc>& x, const AutoPtr<CArgDesc>& y){ return x->GetName() < y->GetName();}///////////////////////////////////////////////////////// CArgDescMandatory::CArgDescMandatory::CArgDescMandatory(const string& name, const string& comment, CArgDescriptions::EType type, CArgDescriptions::TFlags flags) : CArgDesc(name, comment), m_Type(type), m_Flags(flags){ // verify if "flags" "type" are matching switch ( type ) { case CArgDescriptions::eBoolean: case CArgDescriptions::eOutputFile: return; case CArgDescriptions::eInputFile: if((flags & ~(CArgDescriptions::fPreOpen | CArgDescriptions::fBinary)) == 0) return; case CArgDescriptions::k_EType_Size: _TROUBLE; NCBI_THROW(CArgException, eArgType, s_ArgExptMsg(GetName(), "Invalid argument type", "k_EType_Size")); default: if (flags == 0) return; } NCBI_THROW(CArgException, eArgType, s_ArgExptMsg(GetName(), "Argument type/flags mismatch", "(type=" + CArgDescriptions::GetTypeName(type) + ", flags=" + NStr::UIntToString(flags) + ")")); }CArgDescMandatory::~CArgDescMandatory(void){ return;}string CArgDescMandatory::GetUsageCommentAttr(void) const{ // Print type name string str = CArgDescriptions::GetTypeName(GetType()); // Print constraint info, if any string constr = GetUsageConstraint(); if ( !constr.empty() ) { str += ", "; str += constr; } return str;}CArgValue* CArgDescMandatory::ProcessArgument(const string& value) const{ // Process according to the argument type CRef<CArgValue> arg_value; switch ( GetType() ) { case CArgDescriptions::eString: arg_value = new CArg_String(GetName(), value); break; case CArgDescriptions::eBoolean: arg_value = new CArg_Boolean(GetName(), value); break; case CArgDescriptions::eInteger: arg_value = new CArg_Integer(GetName(), value); break; case CArgDescriptions::eDouble: arg_value = new CArg_Double(GetName(), value); break; case CArgDescriptions::eInputFile: { bool delay_open = (GetFlags() & CArgDescriptions::fPreOpen) == 0; IOS_BASE::openmode openmode = (IOS_BASE::openmode) 0; if (GetFlags() & CArgDescriptions::fBinary) openmode |= IOS_BASE::binary; arg_value = new CArg_InputFile(GetName(), value, openmode, delay_open); break; } case CArgDescriptions::eOutputFile: { bool delay_open = (GetFlags() & CArgDescriptions::fPreOpen) == 0; IOS_BASE::openmode openmode = (IOS_BASE::openmode) 0; if (GetFlags() & CArgDescriptions::fBinary) openmode |= IOS_BASE::binary; if (GetFlags() & CArgDescriptions::fAppend) openmode |= IOS_BASE::app; arg_value = new CArg_OutputFile(GetName(), value, openmode,delay_open); break; } case CArgDescriptions::k_EType_Size: { _TROUBLE; NCBI_THROW(CArgException, eArgType, s_ArgExptMsg(GetName(), "Unknown argument type", NStr::IntToString((int)GetType()))); } } /* switch GetType() */ // Check against additional (user-defined) constraints, if any imposed if ( m_Constraint ) { bool err = false; try { if ( !m_Constraint->Verify(value) ) err = true; } catch (...) { err = true; } if ( err ) { NCBI_THROW(CArgException, eConstraint, s_ArgExptMsg(GetName(), "Illegal value, expected " + m_Constraint->GetUsage(),value)); } } return arg_value.Release();}CArgValue* CArgDescMandatory::ProcessDefault(void) const{ NCBI_THROW(CArgException, eNoArg, s_ArgExptMsg(GetName(), "Required argument missing", GetUsageCommentAttr()));}void CArgDescMandatory::SetConstraint(CArgAllow* constraint){ m_Constraint = constraint;}const CArgAllow* CArgDescMandatory::GetConstraint(void) const{ return m_Constraint;}///////////////////////////////////////////////////////// CArgDescOptional::CArgDescOptional::CArgDescOptional(const string& name, const string& comment, CArgDescriptions::EType type, CArgDescriptions::TFlags flags) : CArgDescMandatory(name, comment, type, flags){ return;}CArgDescOptional::~CArgDescOptional(void){ return;}CArgValue* CArgDescOptional::ProcessDefault(void) const{ return new CArg_NoValue(GetName());}///////////////////////////////////////////////////////// CArgDescDefault::CArgDescDefault::CArgDescDefault(const string& name, const string& comment, CArgDescriptions::EType type, CArgDescriptions::TFlags flags, const string& default_value) : CArgDescMandatory(name, comment, type, flags), CArgDescOptional(name, comment, type, flags), m_DefaultValue(default_value){ return;}CArgDescDefault::~CArgDescDefault(void){ return;}CArgValue* CArgDescDefault::ProcessDefault(void) const{ return ProcessArgument(GetDefaultValue());}void CArgDescDefault::VerifyDefault(void) const{ if (GetType() == CArgDescriptions::eInputFile || GetType() == CArgDescriptions::eOutputFile) { return; } // Process, then immediately delete CRef<CArgValue> arg_value(ProcessArgument(GetDefaultValue()));}///////////////////////////////////////////////////////// CArgDescSynopsis::CArgDescSynopsis::CArgDescSynopsis(const string& synopsis) : m_Synopsis(synopsis){ for (string::const_iterator it = m_Synopsis.begin(); it != m_Synopsis.end(); ++it) { if (*it != '_' && !isalnum(*it)) { NCBI_THROW(CArgException,eSynopsis, "Argument synopsis must be alphanumeric: "+ m_Synopsis); } }}//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// CArgDesc_***:: classes for argument descriptions// CArgDesc_Flag : CArgDesc//// CArgDesc_Key : virtual CArgDescMandatory// CArgDesc_KeyOpt : CArgDesc_Key, virtual CArgDescOptional// CArgDesc_KeyDef : CArgDesc_Key, CArgDescDefault//// CArgDesc_Pos : virtual CArgDescMandatory// CArgDesc_PosOpt : CArgDesc_Pos, virtual CArgDescOptional// CArgDesc_PosDef : CArgDesc_Pos, CArgDescDefault/////////////////////////////////////////////////////////// CArgDesc_Flag::CArgDesc_Flag::CArgDesc_Flag(const string& name, const string& comment, bool set_value) : CArgDesc(name, comment), m_SetValue(set_value){ return;}CArgDesc_Flag::~CArgDesc_Flag(void){ return;}string CArgDesc_Flag::GetUsageSynopsis(bool /*name_only*/) const{ return "-" + GetName();}string CArgDesc_Flag::GetUsageCommentAttr(void) const{ return kEmptyStr;}CArgValue* CArgDesc_Flag::ProcessArgument(const string& /*value*/) const{ if ( m_SetValue ) { return new CArg_Boolean(GetName(), true); } else { return new CArg_NoValue(GetName()); }}CArgValue* CArgDesc_Flag::ProcessDefault(void) const{ if ( m_SetValue ) { return new CArg_NoValue(GetName()); } else { return new CArg_Boolean(GetName(), true); }}///////////////////////////////////////////////////////// CArgDesc_Pos::CArgDesc_Pos::CArgDesc_Pos(const string& name, const string& comment, CArgDescriptions::EType type, CArgDescriptions::TFlags flags) : CArgDescMandatory(name, comment, type, flags){ return;}CArgDesc_Pos::~CArgDesc_Pos(void){ return;}string CArgDesc_Pos::GetUsageSynopsis(bool /*name_only*/) const{ return GetName().empty() ? s_ExtraName : GetName();}///////////////////////////////////////////////////////// CArgDesc_PosOpt::CArgDesc_PosOpt::CArgDesc_PosOpt(const string& name, const string& comment, CArgDescriptions::EType type, CArgDescriptions::TFlags flags) : CArgDescMandatory (name, comment, type, flags), CArgDescOptional (name, comment, type, flags), CArgDesc_Pos (name, comment, type, flags){ return;}CArgDesc_PosOpt::~CArgDesc_PosOpt(void){ return;}///////////////////////////////////////////////////////// CArgDesc_PosDef::CArgDesc_PosDef::CArgDesc_PosDef(const string& name, const string& comment, CArgDescriptions::EType type, CArgDescriptions::TFlags flags, const string& default_value) : CArgDescMandatory (name, comment, type, flags), CArgDescOptional (name, comment, type, flags), CArgDescDefault (name, comment, type, flags, default_value), CArgDesc_PosOpt (name, comment, type, flags){ return;}CArgDesc_PosDef::~CArgDesc_PosDef(void){ return;}///////////////////////////////////////////////////////// CArgDesc_Key::CArgDesc_Key::CArgDesc_Key(const string& name, const string& comment, CArgDescriptions::EType type, CArgDescriptions::TFlags flags, const string& synopsis) : CArgDescMandatory(name, comment, type, flags), CArgDesc_Pos (name, comment, type, flags), CArgDescSynopsis(synopsis){ return;}CArgDesc_Key::~CArgDesc_Key(void){ return;}inline string s_KeyUsageSynopsis(const string& name, const string& synopsis, bool name_only){ if ( name_only ) { return '-' + name; } else { return '-' + name + ' ' + synopsis; }}string CArgDesc_Key::GetUsageSynopsis(bool name_only) const{ return s_KeyUsageSynopsis(GetName(), GetSynopsis(), name_only);}///////////////////////////////////////////////////////// CArgDesc_KeyOpt::CArgDesc_KeyOpt::CArgDesc_KeyOpt(const string& name, const string& comment, CArgDescriptions::EType type, CArgDescriptions::TFlags flags, const string& synopsis) : CArgDescMandatory(name, comment, type, flags), CArgDescOptional (name, comment, type, flags), CArgDesc_PosOpt (name, comment, type, flags), CArgDescSynopsis(synopsis)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -