Skip to content

Commit

Permalink
Merge pull request #373 from kerautret/CLI11
Browse files Browse the repository at this point in the history
WIP: CLI11 to replace Boost program option
  • Loading branch information
dcoeurjo committed Sep 21, 2020
2 parents c4b8180 + b2c4437 commit fb1e2b6
Show file tree
Hide file tree
Showing 86 changed files with 13,605 additions and 7,541 deletions.
6 changes: 2 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ addons:
- g++-4.8
- gcc-4.8
- libboost-dev
- libboost-program-options-dev
- libboost-system-dev

before_install:
- DOC="false"; BTYPE=""
- if [ $CONFIG == "DOCUMENTATION" ]; then DOC="true"; fi
Expand Down Expand Up @@ -97,5 +96,4 @@ deploy:
local-dir: "$TRAVIS_BUILD_DIR/html"
on:
branch: master
condition: [$DOC = "true"]

condition: [$DOC = "true"]
38 changes: 23 additions & 15 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ endif (CMAKE_CXX_FLAGS MATCHES "-ftemplate-depth-[0-9]*")



# CLI11
include_directories( "${PROJECT_SOURCE_DIR}/ext/" )



# -----------------------------------------------------------------------------
# Define variables and cmake parameters, and display some information
Expand All @@ -58,27 +62,31 @@ SET(DGTALTOOLS_VERSION "${DGtalTools_VERSION_MAJOR}.${DGtalTools_VERSION_MINOR}.
SET(PROJECT_VERSION "${DGtalTools_VERSION_MAJOR}.${DGtalTools_VERSION_MINOR}.${DGtalTools_VERSION_PATCH}")
SET(VERSION ${DGtalTools_VERSION_MAJOR}.${DGtalTools_VERSION_MINOR}.${DGtalTools_VERSION_PATCH})

OPTION(Boost_USE_STATIC_LIBS "Use boost static lib" ON)
SET(Boost_USE_MULTITHREADED ON)
SET(Boost_USE_STATIC_RUNTIME OFF)
SET(Boost_FOUND FALSE)
FIND_PACKAGE(Boost 1.46.0 REQUIRED COMPONENTS program_options)




# -----------------------------------------------------------------------------
# Looking for boost
# -----------------------------------------------------------------------------

set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
set(Boost_FOUND FALSE)
FIND_PACKAGE(Boost 1.50.0 REQUIRED)
if ( Boost_FOUND )
message(STATUS "Boost and boost_program_options found.")
ADD_DEFINITIONS(${BOOST_DEFINITIONS} -DBOOST_ALL_NO_LIB)
include_directories( ${Boost_INCLUDE_DIRS} )
ADD_DEFINITIONS(${BOOST_DEFINITIONS} -DBOOST_ALL_NO_LIB)
# SYSTEM to avoid warnings from boost.
include_directories(SYSTEM ${Boost_INCLUDE_DIRS} )
SET(DGtalToolsLibDependencies ${DGtalToolsLibDependencies}
${Boost_LIBRAIRIES}
${Boost_PROGRAM_OPTIONS_LIBRARY} Boost::program_options)
SET(DGtalLibInc ${Boost_INCLUDE_DIRS})
## Used to avoid ld warnings (to be removed when using CLI11 instead program_options)
if(APPLE)
set (CMAKE_CXX_FLAGS "-fvisibility=hidden")
endif(APPLE)
${Boost_LIBRAIRIES})
SET(DGtalLibInc ${Boost_INCLUDE_DIRS})
endif( Boost_FOUND )




# -----------------------------------------------------------------------------
# Documentation
# -----------------------------------------------------------------------------
Expand Down
3 changes: 3 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# DGtalTools 1.1

- *global*
- New way to handle command line options of DGtalTools by using CLI11 instead boost program option.
(Bertrand Kerautret and Phuc Ngo
[#373](https://github.com/DGtal-team/DGtalTools/pull/373))
- Fix issue of link with boost program option. (Bertrand Kerautret
[#356](https://github.com/DGtal-team/DGtalTools/pull/356))
- set cmake based CPP11 check instead the manual DGtal check. (Bertrand
Expand Down
71 changes: 28 additions & 43 deletions converters/HDF52vol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,13 @@
#include <DGtal/images/Image.h>
#include <DGtal/images/ImageContainerBySTLVector.h>

#include <boost/program_options/options_description.hpp>
#include <boost/program_options/parsers.hpp>
#include <boost/program_options/variables_map.hpp>
#include "CLI11.hpp"


using namespace std;
using namespace DGtal;
using namespace Z3i;

namespace po = boost::program_options;

/**
@page HDF52vol HDF52vol
Expand All @@ -52,9 +50,14 @@ namespace po = boost::program_options;
@b Allowed @b options @b are:
@code
-h [ --help ] display this message
-i [ --input ] arg the input FreemanChain file name
-o [ --output ] arg (=result.pgm) the output filename
Positionals:
1 TEXT:FILE REQUIRED volumetric file (.pgm3d, .vol, .longvol).
Options:
-h,--help Print this help message and exit
-i,--input TEXT:FILE REQUIRED the input FreemanChain file name
-o,--output TEXT the output filename
@endcode
@b Example:
Expand Down Expand Up @@ -83,49 +86,31 @@ void missingParam ( std::string param )
int main(int argc, char**argv)
{

// parse command line ----------------------------------------------
po::options_description general_opt ( "Allowed options are: " );
general_opt.add_options()
( "help,h", "display this message." )
( "input,i", po::value<std::string>(), "Input HDF5 file." )
( "output,o", po::value<string>(),"Output vol filename." );

bool parseOK=true;
po::variables_map vm;
try{
po::store(po::parse_command_line(argc, argv, general_opt), vm);
}catch(const std::exception& ex){
parseOK=false;
trace.info()<< "Error checking program options: "<< ex.what()<< endl;
}

po::notify ( vm );
if (!parseOK || vm.count ( "help" ) ||argc<=1 )
{
trace.info() << "Convert a 3D 8-bit HDF5 file to vol."<<std::endl
<< std::endl << "Basic usage: "<<std::endl
<< "\tHDF52vol --input <HDF5FileName> --output <VolOutputFileName> "<<std::endl
<< general_opt << "\n";
return 0;
}

//Parse options
if ( ! ( vm.count ( "input" ) ) ) missingParam ( "--input" );
std::string filename = vm["input"].as<std::string>();
if ( ! ( vm.count ( "output" ) ) ) missingParam ( "--output" );
std::string outputFileName = vm["output"].as<std::string>();


// parse command line using CLI ----------------------------------------------
CLI::App app;
std::string inputFileName;
std::string outputFileName {"result.vol"};

app.description("Convert a 3D 8-bit HDF5 file to vol.");
app.add_option("-i,--input,1", inputFileName, "Input HDF5 file." )
->required()
->check(CLI::ExistingFile);
app.add_option("-o,--output,2", outputFileName, "Output vol filename.", true );

app.get_formatter()->column_width(40);
CLI11_PARSE(app, argc, argv);
// END parse command line using CLI ----------------------------------------------

typedef ImageContainerBySTLVector<Z3i::Domain, unsigned char> MyImageC;

MyImageC imageC = HDF5Reader< MyImageC >::importHDF5_3D( filename, "/UInt8Array3D" );
MyImageC imageC = HDF5Reader< MyImageC >::importHDF5_3D( inputFileName, "/UInt8Array3D" );
bool res = VolWriter< MyImageC>::exportVol(outputFileName, imageC);

if (res)
return 0;
return EXIT_SUCCESS;
else
{
trace.error()<< "Error while exporting the volume."<<std::endl;
return 1;
return EXIT_FAILURE;
}
}
82 changes: 28 additions & 54 deletions converters/convertVol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@
#include "DGtal/io/readers/GenericReader.h"
#include "DGtal/io/writers/GenericWriter.h"


#include <boost/program_options/options_description.hpp>
#include <boost/program_options/parsers.hpp>
#include <boost/program_options/variables_map.hpp>
#include "CLI11.hpp"

using namespace std;
using namespace DGtal;
Expand All @@ -46,15 +43,19 @@ using namespace DGtal;
@page convertVol convertVol
@brief Converts volumetric file into volumetric file from different formats (pgm3d, vol, longvol). This tool can also be used to upgrade a Version-2 Vol or Longvol file to the new (compressed) Version-3.
@b Usage: convertVol [input] [output]
@b Allowed @b options @b are:
@code
-h [ --help ] display this message
-i [ --input ] arg volumetric file (.pgm3d, .vol, .longvol)
-o [ --output ] arg volumetric file (.pgm3d, .vol, .longvol)
Positionals:
1 TEXT:FILE REQUIRED volumetric file (.pgm3d, .vol, .longvol).
Options:
-h,--help Print this help message and exit
-i,--input TEXT:FILE REQUIRED volumetric file (.pgm3d, .vol, .longvol).
-o,--output TEXT volumetric file (.pgm3d, .vol, .longvol)
@endcode
@b Examples:
Expand All @@ -73,60 +74,33 @@ using namespace DGtal;
*/


///////////////////////////////////////////////////////////////////////////////
namespace po = boost::program_options;

int main( int argc, char** argv )
{
typedef ImageContainerBySTLVector < Z3i::Domain, unsigned char> Image3D;

// parse command line ----------------------------------------------
po::options_description general_opt("Allowed options are: ");
general_opt.add_options()
("help,h", "display this message")
("input,i", po::value<std::string>(), "volumetric file (.pgm3d, .vol, .longvol) " )
("output,o", po::value<std::string>(), "volumetric file (.pgm3d, .vol, .longvol) " );

// parse command line using CLI ----------------------------------------------
CLI::App app;
std::string inputFileName;
std::string outputFileName {"result.vol"};

bool parseOK=true;
po::variables_map vm;
try{
po::store(po::parse_command_line(argc, argv, general_opt), vm);
}catch(const std::exception& ex){
parseOK=false;
trace.info()<< "Error checking program options: "<< ex.what()<< endl;
}
po::notify(vm);
if( !parseOK || vm.count("help")||argc<=1)
{
std::cout << "Usage: " << argv[0] << " [input] [output]\n"
<< "Convert volumetric file into volumetric file from different formats (pgm3d, vol, longvol) "
<< general_opt << "\n";
std::cout << "Example:\n"
<< "convertVol -i ${DGtal}/examples/samples/lobster.vol -o convertedVol.p3d \n";
return 0;
}

if(! vm.count("input")||! vm.count("output"))
{
trace.error() << " Input and output filename are needed to be defined" << endl;
return 0;
}


string inputFilename = vm["input"].as<std::string>();
string outputFilename = vm["output"].as<std::string>();

trace.info() << "Reading input file " << inputFilename ;
Image3D inputImage = DGtal::GenericReader<Image3D>::import(inputFilename);
app.description("Convert volumetric file into volumetric file from different formats (pgm3d, vol, longvol)\n ");
app.add_option("-i,--input,1", inputFileName, "volumetric file (.pgm3d, .vol, .longvol)." )
->required()
->check(CLI::ExistingFile);
app.add_option("-o,--output,2", outputFileName, "volumetric file (.pgm3d, .vol, .longvol)", true);

app.get_formatter()->column_width(40);
CLI11_PARSE(app, argc, argv);
// END parse command line using CLI ----------------------------------------------

trace.info() << "Reading input file " << inputFileName ;
Image3D inputImage = DGtal::GenericReader<Image3D>::import(inputFileName);
trace.info() << " [done] " << std::endl ;
trace.info() << "Writing output file " << outputFilename ;
DGtal::GenericWriter<Image3D>::exportFile(outputFilename, inputImage);
trace.info() << "Writing output file " << outputFileName ;
DGtal::GenericWriter<Image3D>::exportFile(outputFileName, inputImage);
trace.info() << " [done] " << std::endl ;


return 0;

return EXIT_SUCCESS;
}


Expand Down
Loading

0 comments on commit fb1e2b6

Please sign in to comment.