Thanks to visit codestin.com
Credit goes to Github.com

Skip to content

A simple library to parse command line arguments in C++.

Notifications You must be signed in to change notification settings

p0dalirius/lib-parseargs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

lib-parseargs

A simple library to parse command line arguments in C++.
GitHub release (latest by date) YouTube Channel Subscribers

Note: Use at least c++17.

Features

  • Optional or required arguments, with default values.
  • Positional arguments.
  • Default help option (BooleanSwitch) -h or --help to display help message.
  • Parsing of arguments and printing of help message if missing mandatory arguments.b

image

Example code

#include <iostream>
#include "ArgumentsParser/ArgumentsParser.h"


ArgumentsParser parseArgs(int argc, char* argv[]) {
	ArgumentsParser parser = ArgumentsParser();

	parser.add_positional_string_argument("mode", "Operation mode");
	parser.add_positional_string_argument("another", "Another positional");
	
	parser.add_string_argument("target", "-t", "--target", "", true, "IP or adress of the target machine");
	parser.add_int_argument("port", "-p", "--port", "", true, "Port of the target machine");

	parser.add_boolean_switch_argument("verbose", "-v", "--verbose", false, false, "Verbose mode.");

	parser.parse_args(argc, argv);
	return parser;
}

int main(int argc, char* argv[])
{
	ArgumentsParser parser = parseArgs(argc, argv);

	if (std::get<bool>(parser.get_value("verbose")) == true) {
		std::cout << "[verbose] Mode verbose started.\n";
	}

	std::cout << std::get<int>(parser.get_value("port")) << "\n";

	parser.debug();
}

Output:

C:\Users\dev\> .\PocArgs.exe
Usage: PocArgs.exe <mode> <another> [-p port] [-t target] [-v verbose]

Positional arguments:
   <mode>   Operation mode
   <another>   Another positional

Required arguments:
   -t, --target IP or adress of the target machine
   -p, --port Port of the target machine

Optional arguments:
   -v, --verbose Verbose mode. (default: false)

About

A simple library to parse command line arguments in C++.

Topics

Resources

Stars

Watchers

Forks

Sponsor this project

  •  

Packages

No packages published