2 * Copyright (C) 2010-2013 Toni Gundogdu <legatvs@gmail.com>
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include <boost/filesystem.hpp>
25 #include <boost/foreach.hpp>
28 #define foreach BOOST_FOREACH
39 namespace po = boost::program_options;
40 namespace fs = boost::filesystem;
42 typedef std::vector<std::string> vst;
44 void options::exec(int argc, char **argv)
46 memset(&flags, 0, sizeof(struct flags_s));
50 #if BOOST_FILESYSTEM_VERSION > 2
51 fs::path conf_path(fs::current_path());
53 fs::path conf_path(fs::current_path<fs::path>());
56 const char *home = getenv("HOME");
58 if (home && strlen(home) > 0)
59 conf_path = fs::system_complete(fs::path(home));
65 std::string("ccliverc");
69 po::options_description generic;
70 std::string conf_file;
74 po::value(&flags.print_config)->zero_tokens(),
75 "Print value of defined config options")
77 po::value(&flags.version)->zero_tokens(),
78 "Print version and exit")
80 po::value(&flags.help)->zero_tokens(),
81 "Print help and exit")
83 po::value(&flags.support)->zero_tokens(),
84 "Print supported websites and exit")
86 po::value(&flags.verbose_libcurl)->zero_tokens()->default_value(false),
87 "Turn on libcurl verbose output")
89 po::value(&flags.quiet)->zero_tokens()->default_value(false),
90 "Turn off all output, excl. errors")
93 po::value(&flags.background)->zero_tokens()->default_value(false),
97 po::value(&flags.print_streams)->zero_tokens(),
98 "Print available media streams")
100 po::value<std::string>(),
101 "Select media stream")
103 po::value(&flags.overwrite)->zero_tokens()->default_value(false),
104 "Overwrite existing media")
106 po::value<std::string>(),
107 "Write media to arg")
109 po::value(&flags.no_download)->zero_tokens()->default_value(false),
110 "Do not download media, print details")
112 po::value(&flags.no_proxy)->zero_tokens()->default_value(false),
113 "Do not use HTTP proxy")
115 po::value<std::string>()->default_value("cclive_log"),
116 "Write log output to arg")
118 po::value<std::string>(&conf_file)->default_value(conf_path.string()),
119 "Read args from arg")
124 po::options_description config("Configuration");
128 po::value(&flags.no_resolve)->zero_tokens()->default_value(false),
129 "Do not resolve URL redirections")
131 po::value(&flags.cont)->zero_tokens()->default_value(false),
132 "Resume partially downloaded media")
134 po::value<std::vector<std::string> >()->composing(),
135 "Preferred format [domain:format[,...]]")
137 po::value<std::string>()->default_value("normal"),
138 "Use progressbar arg")
140 po::value<double>()->default_value(1.0),
141 "Update interval of progressbar")
143 po::value<std::string>()->default_value("%t.%s"),
144 "Downloaded media filename format")
146 po::value<std::string>(),
147 "Write downloaded media to arg directory")
149 po::value<vst>()->composing(),
150 "Translate characters in media title")
151 ("exec", po::value<vst>()->composing(),
152 "Invoke arg after each finished download")
154 po::value<std::string>()->default_value("Mozilla/5.0"),
155 "Identify as arg to HTTP servers")
156 ("proxy", po::value<std::string>(),
157 "Use proxy for HTTP connections")
158 ("throttle", po::value<int>()->default_value(0),
159 "Do not exceed transfer rate arg KB/s")
160 ("connect-timeout", po::value<int>()->default_value(30),
161 "Seconds connecting allowed to take")
162 ("transfer-timeout", po::value<int>()->default_value(0),
163 "Seconds transfer allowed to take")
164 ("dns-cache-timeout", po::value<int>()->default_value(60),
165 "Seconds DNS resolves kept in memory")
166 ("max-retries", po::value<int>()->default_value(5),
167 "Max download attempts before giving up")
168 ("retry-wait", po::value<int>()->default_value(5),
169 "Time to wait before retrying")
174 po::options_description hidden;
177 ("url", po::value<vst>(), "url");
181 _visible.add(generic).add(config);
183 // Command line options.
185 po::options_description cmdline_options;
186 cmdline_options.add(generic).add(config).add(hidden);
188 // Config file options.
190 po::options_description config_file_options;
191 config_file_options.add(config);
195 po::positional_options_description p;
200 store(po::command_line_parser(argc,argv)
201 .options(cmdline_options).positional(p).run(), _map);
206 std::ifstream ifs(conf_file.c_str());
210 store(parse_config_file(ifs, config_file_options), _map);
217 void options::_validate()
221 if (_map.count("tr"))
223 vst v = _map["tr"].as<vst>();
224 foreach (const std::string s, v)
233 for (po::variables_map::iterator i = _map.begin(); i != _map.end(); ++i)
235 const po::variable_value &v = i->second;
240 const std::type_info &t = v.value().type();
243 if (t == typeid(bool))
244 std::cout << i->first << " is " << (v.as<bool>() ? "set" : "unset");
245 else if (t == typeid(vst))
247 const vst r = v.as<vst>();
248 foreach (const std::string s, r)
250 std::cout << i->first << "=" << s << "\n";
256 std::cout << i->first << "=";
257 if (t == typeid(std::string))
258 std::cout << v.as<std::string>();
259 else if (t == typeid(double))
260 std::cout << v.as<double>();
261 else if (t == typeid(int))
262 std::cout << v.as<int>();
264 std::cout << "<unsupported type error>";
268 std::cout << std::flush;
270 std::cout << std::endl;
276 // vim: set ts=2 sw=2 tw=72 expandtab: