Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
generic_opts.cpp
Go to the documentation of this file.
1// -------------------------------------------------------//
2//
3// SHAMROCK code for hydrodynamics
4// Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me>
5// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1
6// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information
7//
8// -------------------------------------------------------//
9
16
19#include "shambase/print.hpp"
20#include "shambase/string.hpp"
22#include "sham/term/env.hpp"
23#include "sham/term/tty.hpp"
24#include "shamcmdopt/cmdopt.hpp"
26#include "shamcmdopt/env.hpp"
27#include <string_view>
28#include <optional>
29#include <stdexcept>
30#include <string>
31#include <vector>
32
33namespace {
34
35 auto term_parse_error_callback(const char *what, std::source_location where)
36 -> std::invalid_argument {
38 };
39
40} // namespace
41
42namespace shamcmdopt {
43
45
46 register_opt("--nocolor", {}, "disable colored output");
47 register_opt("--color", {}, "force colored output");
48 register_opt("--help", {}, "show this message");
49
50 register_env_var_doc("NO_COLOR", "Disable colors (if no color cli args are passed)");
51 register_env_var_doc("CLICOLOR_FORCE", "Enable colors (if no color cli args are passed)");
52 register_env_var_doc("TERM", "Terminal emulator identifier");
53 register_env_var_doc("COLORTERM", "Terminal color support identifier");
54 register_env_var_doc("COLUMN", "Set tty assumed column count");
55 }
56
72
73 bool has_opt_nocolor = has_option("--nocolor");
74 bool has_opt_color = has_option("--color");
75
76 if (has_opt_color && has_opt_nocolor) {
78 "You can not pass --nocolor and --color simultaneously");
79 }
80
81 auto TERM = getenv_str_view("TERM");
82 auto COLORTERM = getenv_str_view("COLORTERM");
83 auto NO_COLOR = getenv_str_view("NO_COLOR");
84 auto CLICOLOR_FORCE = getenv_str_view("CLICOLOR_FORCE");
85
86 auto COLUMN = getenv_str_view("COLUMN");
87
89 {
90 .TERM = TERM,
91 .COLORTERM = COLORTERM,
92 .NO_COLOR = NO_COLOR,
93 .CLICOLOR_FORCE = CLICOLOR_FORCE,
94 .COLUMN = COLUMN,
95 },
96 term_parse_error_callback);
97
98 if (has_opt_nocolor) {
100 } else if (has_opt_color) {
102 }
103 }
104
106
108
109 if (has_option("--help")) {
110 print_help();
111
112 shambase::println("\nEnv deduced vars :");
113
114 if (sham::term::is_a_tty()) {
115 shambase::println(" isatty = Yes");
116 } else {
117 shambase::println(" isatty = No");
118 }
119
121 shambase::println(" color = enabled");
122 } else {
123 shambase::println(" color = disabled");
124 }
125
127 shambase::format(
128 " tty size = {}x{}",
131 }
132 }
133
134} // namespace shamcmdopt
Source location utility.
This header file contains utility functions related to exception handling in the code.
This file handler generic cli & env options.
void println(std::string_view sv)
Prints a string to the console followed by a newline.
Definition print.cpp:37
ExcptTypes make_except_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Create an exception with a message and a location.
namespace for cli utilities
Definition ci_env.hpp:19
void process_cmdopt_generic_opts()
Process generic cli and env variables options.
bool has_option(const std::string_view &option_name)
Check if an option is present.
Definition cmdopt.cpp:128
void register_cmdopt_generic_opts()
Register generic cli and env variables options.
void process_env_vars()
Detect if the current process should use colored output or not.
void register_env_var_doc(std::string env_var, std::string desc)
Register the documentation of an environment variable.
Definition env.cpp:43
std::optional< std::string_view > getenv_str_view(const char *env_var)
Get the content of the environment variable if it exist.
Definition env.cpp:32
void register_opt(std::string name, std::optional< std::string > args, std::string description)
Register a command line option.
Definition cmdopt.cpp:165
void print_help()
Print the help message.
Definition cmdopt.cpp:211
Environment variable parsing for terminal color and size configuration (TERM, COLORTERM,...
void parse_terminal_support(TermEnvVars vars, const term_parse_callback_t &error_callback)
Parses terminal environment variables to determine color support and set terminal size.
Definition env.cpp:73
provide information about the source location
bool colors_enabled()
Are colors enabled.
void disable_colors()
Disable all colors.
void enable_colors()
Enable colors in logs.
This file contains tty info getters.
int get_tty_columns()
Get the number of columns of the current terminal.
Definition tty.cpp:81
int get_tty_lines()
Get the number of lines of the current terminal.
Definition tty.cpp:82
bool is_a_tty()
Test if current terminal is a tty.
Definition tty.cpp:31