diff options
author | Sergey Bugaev <bugaevc@serenityos.org> | 2020-01-27 20:25:36 +0300 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-01-28 13:50:18 +0100 |
commit | f983dfe319978b47b4c5314df9ea3373e3d69c05 (patch) | |
tree | 27d599f2c30ca6bfbfee6fbc3df98a08f33ac887 /Userland/head.cpp | |
parent | 92765825358daaed11fa105ca64cf2c7819e5563 (diff) | |
download | serenity-f983dfe319978b47b4c5314df9ea3373e3d69c05.zip |
Userland+Terminal: Port to new CArgsParser API
While at it, also add some niceties and fix some things.
Diffstat (limited to 'Userland/head.cpp')
-rw-r--r-- | Userland/head.cpp | 53 |
1 files changed, 13 insertions, 40 deletions
diff --git a/Userland/head.cpp b/Userland/head.cpp index 84ccd942c3..aaeac55fa5 100644 --- a/Userland/head.cpp +++ b/Userland/head.cpp @@ -35,56 +35,29 @@ int head(const String& filename, bool print_filename, int line_count, int char_c int main(int argc, char** argv) { - CArgsParser args_parser("head"); - - args_parser.add_arg("n", "lines", "Number of lines to print (default 10)"); - args_parser.add_arg("c", "characters", "Number of characters to print"); - args_parser.add_arg("q", "Never print filenames"); - args_parser.add_arg("v", "Always print filenames"); - - CArgsParserResult args = args_parser.parse(argc, argv); - int line_count = 0; - if (args.is_present("n")) { - line_count = strtol(args.get("n").characters(), NULL, 10); - if (errno) { - args_parser.print_usage(); - return -1; - } - - if (!line_count) { - args_parser.print_usage(); - return -1; - } - } - int char_count = 0; - if (args.is_present("c")) { - char_count = strtol(args.get("c").characters(), NULL, 10); - if (errno) { - args_parser.print_usage(); - return -1; - } - - if (!char_count) { - args_parser.print_usage(); - return -1; - } - } + bool never_print_filenames = false; + bool always_print_filenames = false; + Vector<const char*> files; + + CArgsParser args_parser; + args_parser.add_option(line_count, "Number of lines to print (default 10)", "lines", 'n', "number"); + args_parser.add_option(char_count, "Number of characters to print", "characters", 'c', "number"); + args_parser.add_option(never_print_filenames, "Never print file names", "quiet", 'q'); + args_parser.add_option(always_print_filenames, "Always print file names", "verbose", 'v'); + args_parser.add_positional_argument(files, "File to process", "file", CArgsParser::Required::No); + args_parser.parse(argc, argv); if (line_count == 0 && char_count == 0) { line_count = 10; } - Vector<String> files = args.get_single_values(); - bool print_filenames = files.size() > 1; - - if (args.is_present("v")) { + if (always_print_filenames) print_filenames = true; - } else if (args.is_present("q")) { + else if (never_print_filenames) print_filenames = false; - } if (files.is_empty()) { return head("", print_filenames, line_count, char_count); |