MCPcopy Create free account
hub / github.com/GoogleCloudPlatform/cpp-samples / ParseArguments

Function ParseArguments

speech/api/parse_arguments.cc:25–92  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

23namespace po = ::boost::program_options;
24
25ParseResult ParseArguments(int argc, char* argv[]) {
26 po::positional_options_description positional;
27 positional.add("path", 1);
28 po::options_description desc("A Speech-to-Text transcription example");
29 desc.add_options()("help", "produce help message")
30 //
31 ("bitrate", po::value<int>()->default_value(16000),
32 "the sample rate in Hz")
33 //
34 ("language-code", po::value<std::string>()->default_value("en"),
35 "the language code for the audio")
36 //
37 ("path", po::value<std::string>()->required(),
38 "the name of an audio file to transcribe. Prefix the path with gs:// to "
39 "use objects in GCS.");
40 po::variables_map vm;
41 po::store(po::command_line_parser(argc, argv)
42 .options(desc)
43 .positional(positional)
44 .run(),
45 vm);
46 po::notify(vm);
47
48 auto const path = vm["path"].as<std::string>();
49 auto const bitrate = vm["bitrate"].as<int>();
50 auto const language_code = vm["language-code"].as<std::string>();
51 // Validate the command-line options.
52 if (path.empty()) {
53 throw std::runtime_error("The audio file path cannot be empty");
54 }
55 if (bitrate < 0) {
56 throw std::runtime_error(
57 "--bitrate option must be a positive number, value=" +
58 std::to_string(bitrate));
59 }
60
61 ParseResult result;
62 result.path = path;
63 result.config.set_language_code(language_code);
64 result.config.set_sample_rate_hertz(bitrate);
65
66 // Use the audio file extension to configure the encoding.
67 auto const ext = [&] {
68 auto e = result.path.rfind('.');
69 if (e == std::string::npos) return std::string{};
70 auto ext = result.path.substr(e);
71 std::transform(ext.begin(), ext.end(), ext.begin(),
72 [](char c) { return static_cast<char>(std::tolower(c)); });
73 return ext;
74 }();
75
76 if (ext.empty() || ext == ".raw") {
77 result.config.set_encoding(RecognitionConfig::LINEAR16);
78 } else if (ext == ".ulaw") {
79 result.config.set_encoding(RecognitionConfig::MULAW);
80 } else if (ext == ".flac") {
81 result.config.set_encoding(RecognitionConfig::FLAC);
82 } else if (ext == ".amr") {

Callers 5

mainFunction · 0.70
mainFunction · 0.70
mainFunction · 0.70
mainFunction · 0.70
mainFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected