Add Dirk Luetjen's ssphys libraries and command-line tool
[vss2svn.git] / ssphys / SSPhys / CommandLine.cpp
bloba6bf2feeff20e7b388b02da4deae58ec5084822d
1 // CommandLine.cpp: implementation of the CCommandLine class.
2 //
3 //////////////////////////////////////////////////////////////////////
5 #include "stdafx.h"
6 #include "CommandLine.h"
7 #include "Command.h"
9 //---------------------------------------------------------------------------
10 tristate ToTristate (const char ch)
12 if (!ch)
13 return undefined;
14 if (ch == '-')
15 return cleared;
16 else if (ch == '+')
17 return set;
19 return undefined;
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 ();
42 COption option;
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:
52 if (++i < argc)
53 option.value = argv[i];
54 else
55 throw SSException ("missing command line parameter");
56 break;
57 case COptionInfo::optionalArgument:
58 if (++i < argc)
59 option.value = argv[i];
60 break;
61 case COptionInfo::tristateArgument:
62 option.value = ToTristate (argv[i][2]);
63 break;
66 break;
68 ++itor;
71 if (itor == m_OptionsInfo.end ())
72 throw SSException ("unknown command line parameter");
74 m_Options.push_back (option);
76 else
78 if (m_Command.empty ())
80 m_Command = argv[i];
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 ());
85 else
87 m_Args.push (argv[i]);
94 bool CCommandLine::IsLongArgument (const char* ch, const char* arg)
96 if (!ch)
97 return false;
99 std::string str = (ch[0] == '-') ? &ch[1] : &ch[0];
100 if (str == arg)
101 return true;
103 return false;
106 bool CCommandLine::IsArgChar (const char ch, const char arg)
108 if (!ch)
109 return false;
110 else if (ch == arg)
111 return true;
112 else if (tolower(ch) == arg)
113 return true;
114 else if (toupper(ch) == arg)
115 return true;
116 return false;