* Path for renames during restore and renames during share (thanks to Bryan Aldrich...
[vss2svn.git] / ssphys / SSPhys / Command.cpp
blob8eb71d9c68a0f8562ab072f9aaa5808a139ce7b5
1 // Command.cpp: implementation of the CCommand class.
2 //
3 //////////////////////////////////////////////////////////////////////
5 #include "StdAfx.h"
6 #include "Command.h"
7 #include <stdexcept>
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)
55 .options(descr)
56 .positional(GetPositionalOptionsDescription ())
57 .extra_parser(cmd_line_utils::vss_option_parser (descr))
58 .run();
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 ())
70 // {
71 // try {
72 // if (namesCache.Open (options.GetNamesCache ()))
73 // g_NamesCache.SetFile (&namesCache);
74 // }
75 // catch (SSException& ex)
76 // {
77 // std::cerr << "names cache error: " << ex.what() << std::endl;
78 // }
79 // }
81 Execute(m_VariablesMap, arguments);
83 return 0;
86 std::auto_ptr<CFormatter>& CCommand::GetFormatter ()
88 if (!m_pFormatter.get ())
89 m_pFormatter = CFormatterFactory::MakeFormatter (m_VariablesMap);
90 return m_pFormatter;
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());
102 descr.add_options ()
103 ("input", po::value <std::vector <std::string> > (), "input file");
104 return descr;
107 po::positional_options_description CMultiArgCommand::GetPositionalOptionsDescription () const
109 po::positional_options_description positional (CCommand::GetPositionalOptionsDescription());
110 positional.add ("input", -1);
111 return positional;
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);
123 else
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)
129 // {
130 // Execute (options, *citor);
131 // }