Skip to content

Commit

Permalink
Added examples for binding functions and methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafał Długołęcki committed Feb 8, 2016
1 parent 6029eb4 commit 5d3d8cf
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 3 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## [0.3] - 2016-02-08
### Added
- Possibility to bind class method to parameter
- Examples (check examples dir)
2 changes: 1 addition & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ CLEANFILES = \
%~ \
doxyfile.stamp

SUBDIRS = include . tests docs
SUBDIRS = include . tests examples docs

noinst_PROGRAMS = bin/command

Expand Down
3 changes: 2 additions & 1 deletion configure.ac
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
AC_INIT([Command], [0.2.1], [bugz@dlugolecki.net.pl], [command], [http://dlugolecki.net.pl/software/command/])
AC_INIT([Command], [0.3], [bugz@dlugolecki.net.pl], [command], [http://dlugolecki.net.pl/software/command/])
AC_PREREQ([2.59])
AM_INIT_AUTOMAKE([1.10 -Wall])
AC_CONFIG_HEADERS([config.h])
Expand All @@ -7,6 +7,7 @@ AC_CONFIG_FILES([
include/Makefile
Makefile
tests/Makefile
examples/Makefile
docs/Makefile
])

Expand Down
9 changes: 8 additions & 1 deletion debian/changelog
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
command (0.3) unstable; urgency=medium

* Possibility to bind class method to parameter
* New examples

-- Rafał Długołęcki <rafal@dlugolecki.net.pl> Mon, 08 Feb 2016 22:06:13 +0100

command (0.2.1) unstable; urgency=medium

* Initial public release.
* Added debian package building configuration

-- Rafał Długołęcki <rafal@yggdrasil> Wed, 20 May 2015 21:52:46 +0200
-- Rafał Długołęcki <rafal@dlugolecki.net.pl> Wed, 20 May 2015 21:52:46 +0200
18 changes: 18 additions & 0 deletions examples/Makefile.am
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
noinst_PROGRAMS = \
functions \
methods

AM_CXXFLAGS = -std=c++11

functions_CPPFLAGS = \
-I$(top_srcdir)/include \
-Wall -pedantic -Wextra
functions_SOURCES = \
functions.cpp

methods_CPPFLAGS = \
-I$(top_srcdir)/include \
-Wall -pedantic -Wextra
methods_SOURCES = \
methods.cpp

55 changes: 55 additions & 0 deletions examples/functions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <iostream>
#include <string>
#include <functional>

#include "option.h"
#include "argument.h"
#include "required.h"
#include "multiValue.h"
#include "grouped.h"
#include "command.h"

using namespace command;

/**
* @file
* @brief Simple example explaining how to bind functions to command-line parameters
*/

void argument_function(bool a) {
std::cout << "Argument: " << a << std::endl;
}

void option_function(std::string a) {
std::cout << "Help function " << a << std::endl;
}

void void_function(void) {
std::cout << "Void function " << std::endl;
}

int main(int argc, char **argv)
{
try {
Command command(argc, argv, {
// function with bool parameter
new Argument<bool>("Input values", argument_function),

// function with std::string parameter
new Option<std::string>("f", "Optional file", option_function),

// function without parameter
new Option<void>("h", "Help", void_function)
});

/* Code is initialized.
*
* You can run your main program e.g. here
*/
}
catch(const std::exception & e) {
std::cout << e.what() << std::endl;
}

return 0;
}
58 changes: 58 additions & 0 deletions examples/methods.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <iostream>
#include <string>
#include <functional>

#include "option.h"
#include "argument.h"
#include "required.h"
#include "multiValue.h"
#include "grouped.h"
#include "command.h"

using namespace command;

/**
* @file
* @brief Simple example explaining how to bind class methods to command-line parameters
*/

class ExampleClass {
public:
void _argument(bool a) {
std::cout << "Argument: " << a << std::endl;
}
void _option(std::string a) {
std::cout << "Help function " << a << std::endl;
}
void _void(void) {
std::cout << "Void function " << std::endl;
}
};

int main(int argc, char **argv)
{
ExampleClass c;

try {
Command command(argc, argv, {
// class method with bool parameter
new Argument<bool>("Input values", std::bind(&ExampleClass::_argument, &c, std::placeholders::_1)),

// class method std::string parameter
new Option<std::string>("f", "Optional file", std::bind(&ExampleClass::_option, &c, std::placeholders::_1)),

// class method without parameter
new Option<void>("h", "Help", std::bind(&ExampleClass::_void, &c))
});

/* Code is initialized.
*
* You can run your main program for example here
*/
}
catch(const std::exception & e) {
std::cout << e.what() << std::endl;
}

return 0;
}

0 comments on commit 5d3d8cf

Please sign in to comment.