1 // Command.cpp: implementation of the CCommand class.
3 //////////////////////////////////////////////////////////////////////
9 //////////////////////////////////////////////////////////////////////
10 // Construction/Destruction
11 //////////////////////////////////////////////////////////////////////
13 CCommand::CCommand (std::string commandName
, std::string description
)
14 : m_CommandName (commandName
),
15 m_CommandDescription (description
)
19 CCommand::~CCommand ()
23 po::options_description
CCommand::GetOptionsDescription () const
25 return po::options_description ("Command options");
27 po::options_description
CCommand::GetHiddenDescription () const
29 return po::options_description ("Hidden command options");
32 po::positional_options_description
CCommand::GetPositionalOptionsDescription () const
34 return po::positional_options_description ();
38 void CCommand::PrintUsage () const
40 std::cout
<< GetCommandDescription () << std::endl
<< std::endl
;
42 po::options_description
descr (GetOptionsDescription ());
43 descr
.add (CFormatterFactory::GetOptionsDescription());
45 std::cout
<< descr
<< std::endl
;
48 int CCommand::Execute (std::vector
<std::string
> const& args
)
50 po::options_description
descr (GetOptionsDescription ());
51 descr
.add (GetHiddenDescription ());
52 descr
.add (CFormatterFactory::GetOptionsDescription());
54 po::parsed_options opts
= po::command_line_parser(args
)
56 .positional(GetPositionalOptionsDescription ())
57 .extra_parser(cmd_line_utils::vss_option_parser (descr
))
59 po::store (opts
, m_VariablesMap
);
60 po::notify(m_VariablesMap
);
62 // extract the arguments from the parsed command line
63 std::vector
<po::option
> arguments
;
64 std::remove_copy_if(opts
.options
.begin(), opts
.options
.end(),
65 std::back_inserter(arguments
), cmd_line_utils::is_argument());
67 // Load the names cache
68 // SSNamesCacheFile namesCache;
69 // if (!options.GetNamesCache ().empty ())
72 // if (namesCache.Open (options.GetNamesCache ()))
73 // g_NamesCache.SetFile (&namesCache);
75 // catch (SSException& ex)
77 // std::cerr << "names cache error: " << ex.what() << std::endl;
81 Execute(m_VariablesMap
, arguments
);
86 std::auto_ptr
<CFormatter
>& CCommand::GetFormatter ()
88 if (!m_pFormatter
.get ())
89 m_pFormatter
= CFormatterFactory::MakeFormatter (m_VariablesMap
);
93 //////////////////////////////////////////////////////////////////////
94 CMultiArgCommand::CMultiArgCommand (std::string commandName
, std::string description
)
95 : CCommand (commandName
, description
)
99 po::options_description
CMultiArgCommand::GetHiddenDescription () const
101 po::options_description
descr (CCommand::GetHiddenDescription());
103 ("input", po::value
<std::vector
<std::string
> > (), "input file");
107 po::positional_options_description
CMultiArgCommand::GetPositionalOptionsDescription () const
109 po::positional_options_description
positional (CCommand::GetPositionalOptionsDescription());
110 positional
.add ("input", -1);
114 void CMultiArgCommand::Execute (po::variables_map
const& options
, std::vector
<po::option
> const& args
)
116 if (options
.count("input") > 0)
118 std::vector
<std::string
> const& args
= options
["input"].as
<std::vector
<std::string
> > ();
119 std::vector
<std::string
>::const_iterator end
= args
.end ();
120 for (std::vector
<std::string
>::const_iterator citor
= args
.begin (); citor
!= end
; ++citor
)
121 Execute (options
, *citor
);
125 throw std::runtime_error("missing argument");
127 // std::vector<po::option>::const_iterator end = args.end();
128 // for (std::vector<po::option>::const_iterator citor = args.begin (); citor != end; ++citor)
130 // Execute (options, *citor);