Skip to content

Instantly share code, notes, and snippets.

View mostsignificant's full-sized avatar
🚀

Christian Göhring mostsignificant

🚀
View GitHub Profile
@mostsignificant
mostsignificant / convo.py
Last active December 6, 2022 00:36
Converts JSON files to other formats via templates
"""
Converts JSON files to other formats via templates.
"""
import argparse
import json
import os
from jinja2 import Environment, FileSystemLoader
@mostsignificant
mostsignificant / cxxopts.cpp
Last active October 20, 2021 21:37
blog-cxxopts
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"});
@mostsignificant
mostsignificant / getopt-example.cpp
Last active October 20, 2021 21:40
blog-getopt-example
#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);
@mostsignificant
mostsignificant / get-and-has-option-example.cpp
Created October 20, 2021 19:28
blog-get-and-has-option-example
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");
@mostsignificant
mostsignificant / has-option.cpp
Last active October 20, 2021 19:19
blog-has-option
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;
}
@mostsignificant
mostsignificant / get-option.cpp
Last active October 19, 2021 22:50
blog-get-option
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 "";
@mostsignificant
mostsignificant / boost-program-options-example.cpp
Created October 19, 2021 22:28
blog-boost-program-options-example
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;
@mostsignificant
mostsignificant / cxxopts-example.cpp
Created October 19, 2021 22:22
blog-cxxopts-example
#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>())
@mostsignificant
mostsignificant / file-name-loop.cpp
Created October 18, 2021 23:38
blog-file-name-loop
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++ << " ";
// ...
@mostsignificant
mostsignificant / main-try-and-catch.cpp
Last active October 18, 2021 23:34
blog-main-try-and-catch
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;
}