1 // CommandLine.cpp: implementation of the CCommandLine class.
3 //////////////////////////////////////////////////////////////////////
6 #include "CommandLine.h"
9 //---------------------------------------------------------------------------
10 tristate
ToTristate (const char ch
)
23 //////////////////////////////////////////////////////////////////////
24 // Construction/Destruction
25 //////////////////////////////////////////////////////////////////////
26 CCommandLine::CCommandLine ()
30 CCommandLine::~CCommandLine ()
35 void CCommandLine::Parse (int argc
, char* argv
[])
37 for (int i
= 1; i
< argc
; ++i
)
39 if (argv
[i
][0] == '-')
41 COptionInfoList::const_iterator itor
= m_OptionsInfo
.begin ();
43 while (itor
!= m_OptionsInfo
.end ())
45 const COptionInfo
& info
= *itor
;
46 if (IsArgChar (argv
[i
][1], info
.m_shortOption
) || IsLongArgument (&argv
[i
][1], info
.m_longOption
.c_str()))
48 option
.id
= info
.m_Id
;
49 switch (info
.m_needArg
)
51 case COptionInfo::requiredArgument
:
53 option
.value
= argv
[i
];
55 throw SSException ("missing command line parameter");
57 case COptionInfo::optionalArgument
:
59 option
.value
= argv
[i
];
61 case COptionInfo::tristateArgument
:
62 option
.value
= ToTristate (argv
[i
][2]);
71 if (itor
== m_OptionsInfo
.end ())
72 throw SSException ("unknown command line parameter");
74 m_Options
.push_back (option
);
78 if (m_Command
.empty ())
81 std::auto_ptr
<CCommand
> pCommand (m_pCommandFactory
->MakeCommand (m_Command
));
82 COptionInfoList commandOptions
= pCommand
->GetOptionsInfo ();
83 m_OptionsInfo
.insert (m_OptionsInfo
.end (), commandOptions
.begin (), commandOptions
.end ());
87 m_Args
.push (argv
[i
]);
94 bool CCommandLine::IsLongArgument (const char* ch
, const char* arg
)
99 std::string str
= (ch
[0] == '-') ? &ch
[1] : &ch
[0];
106 bool CCommandLine::IsArgChar (const char ch
, const char arg
)
112 else if (tolower(ch
) == arg
)
114 else if (toupper(ch
) == arg
)