This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Converts JSON files to other formats via templates. | |
""" | |
import argparse | |
import json | |
import os | |
from jinja2 import Environment, FileSystemLoader |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int main(int argc, char *argv[]) { | |
cxxopts::Options options("dog"); | |
options.add_options() | |
("n,number", "Show line numbers") | |
("E,show-ends", "Show line endings") | |
("version", "Show the version") | |
("input_files", "Input file(s) to concatenate", cxxopts::value<std::vector<std::string>>()); | |
options.parse_positional({"input_files"}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <getopt.h> | |
int main(int argc, char* argv[]) { | |
option longopts[] = { | |
{"number", optional_argument, NULL, 'n'}, | |
{"show-ends", optional_argument, NULL, 'E'}, {0}}; | |
while (1) { | |
const int opt = getopt_long(argc, argv, "nE::", longopts, 0); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const std::vector<std::string_view> args(argv, argv + argc); | |
const bool modify = has_option(args, "-m"); | |
const std::string_view date = get_option(args, "-d"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bool has_option( | |
const std::vector<std::string_view>& args, | |
const std::string_view& option_name) { | |
for (auto it = args.begin(), end = args.end(); it != end; ++it) { | |
if (*it == option_name) | |
return true; | |
} | |
return false; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
std::string_view get_option( | |
const std::vector<std::string_view>& args, | |
const std::string_view& option_name) { | |
for (auto it = args.begin(), end = args.end(); it != end; ++it) { | |
if (*it == option_name) | |
if (it + 1 != end) | |
return *(it + 1); | |
} | |
return ""; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace po = boost::program_options; | |
// Declare the supported options. | |
po::options_description desc("Allowed options"); | |
desc.add_options() | |
("help", "produce help message") | |
("compression", po::value<int>(), "set compression level") | |
; | |
po::variables_map vm; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cxxopts.hpp> | |
// ... | |
cxxopts::Options options("MyProgram", "One line description of MyProgram"); | |
options.add_options() | |
("d,debug", "Enable debugging") // a bool parameter | |
("i,integer", "Int param", cxxopts::value<int>()) | |
("f,file", "File name", cxxopts::value<std::string>()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for (const auto &file_name : program_options::input_files()) { | |
std::ifstream input_file(file_name, std::ios::in); | |
// ... | |
while (std::getline(input_file, line)) { | |
if (program_options::show_line_numbers()) | |
std::cout << std::setw(6) << std::setfill(' ') << line_count++ << " "; | |
// ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try { | |
program_options::parse(argc, argv); | |
} catch (const std::exception &x) { | |
std::cerr << "dog: " << x.what() << '\n'; | |
std::cerr << "usage: dog [-n|--number] [-E|--show-ends] <input_file> ...\n"; | |
return EXIT_FAILURE; | |
} |
NewerOlder