Improved command line interface.
Added [] overloaded operator for accessing ArgumentParser option values. Implemented analysis program option and added checks for lack of arguments. Added arguments for specifying seperate audio and database directories. Implemented automatic setting of database directory as the same as the audio directory if no value is provided on the command line.
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "boost/program_options.hpp"
|
||||
|
||||
using std::vector;
|
||||
using std::string;
|
||||
namespace po = boost::program_options;
|
||||
|
||||
class ArgumentParser {
|
||||
@@ -10,12 +14,25 @@ class ArgumentParser {
|
||||
ArgumentParser(const ArgumentParser&);
|
||||
ArgumentParser& operator=(const ArgumentParser&);
|
||||
int parseargs(int argc, char** argv);
|
||||
po::variables_map::size_type count(char const *ref);
|
||||
|
||||
const po::variable_value& operator [](char const *b) const;
|
||||
//std::string& operator [](char const *b);
|
||||
|
||||
private:
|
||||
// Stores values for arguments parsed from the command line
|
||||
po::variables_map vm;
|
||||
//Create a positional options object for parsing input, output etc
|
||||
//positional arguments from command line.
|
||||
po::positional_options_description positionalOptions;
|
||||
//
|
||||
po::variables_map vm;
|
||||
po::options_description desc;
|
||||
};
|
||||
|
||||
class ConcatenatorArgParse : public ArgumentParser {
|
||||
public:
|
||||
vector<string> get_analyses() { return (*this)["analyses"].as<vector<string>>(); }
|
||||
string get_source_db() { return (*this)["source"].as<string>(); }
|
||||
string get_target_db() { return (*this)["target"].as<string>(); }
|
||||
string get_tar_audio_dir() { return ((*this)["tar_db"].empty() ? (*this)["target"].as<string>() : (*this)["tar_db"].as<string>()); }
|
||||
string get_src_audio_dir() { return ((*this)["src_db"].empty() ? (*this)["source"].as<string>() : (*this)["src_db"].as<string>()); }
|
||||
};
|
||||
|
||||
@@ -9,6 +9,7 @@ using std::string;
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
using std::list;
|
||||
using std::vector;
|
||||
|
||||
/*!
|
||||
* A class that encapsulates a collection of AudioFile objects in order to
|
||||
@@ -17,7 +18,7 @@ using std::list;
|
||||
|
||||
class AudioDatabase {
|
||||
public:
|
||||
AudioDatabase(const std::string audio_dir, const std::string database_dir, list<string>& analyses, Logger* log);
|
||||
AudioDatabase(const std::string database_dir, vector<string>& analyses, Logger* log, const std::string audio_dir="");
|
||||
private:
|
||||
std::string database_dir = "";
|
||||
std::string audio_dir = "";
|
||||
|
||||
+1
-1
@@ -10,7 +10,7 @@
|
||||
class Logger {
|
||||
public:
|
||||
Logger();
|
||||
~Logger() {};
|
||||
~Logger();
|
||||
void trace(std::string str);
|
||||
void debug(std::string str);
|
||||
void info(std::string str);
|
||||
|
||||
+29
-5
@@ -1,29 +1,53 @@
|
||||
#include "ArgumentParser.h"
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
ArgumentParser::ArgumentParser() : desc("Allowed options") {
|
||||
// Add positional arguments to specify source, target and output database locations.
|
||||
positionalOptions.add("source_db", 1);
|
||||
positionalOptions.add("target_db", 1);
|
||||
positionalOptions.add("output_db", 1);
|
||||
positionalOptions.add("source", 1);
|
||||
positionalOptions.add("target", 1);
|
||||
positionalOptions.add("output", 1);
|
||||
|
||||
// Add optional arguments to allow control over application settings from the command line.
|
||||
desc.add_options()
|
||||
("help,h", "produce help message")
|
||||
("compression", po::value<int>(), "set compression level")
|
||||
("source", po::value<string>()->required(), "Source location")
|
||||
("target", po::value<string>()->required(), "Target location")
|
||||
("output", po::value<string>()->required(), "Output location")
|
||||
("analyses,a", po::value<vector<string>>()->multitoken(), "Analysis "
|
||||
"strings specifying analyses to use for database comparison.")
|
||||
("tar_db", po::value<string>(), "Specifies the "
|
||||
"directory to create the target database and store analyses in. If "
|
||||
"not specified then the target directory will be used directly.")
|
||||
("src_db", po::value<string>(), "Specifies the "
|
||||
"directory to create the source database and store analyses in. If "
|
||||
"not specified then the " "source directory will be used directly.")
|
||||
;
|
||||
}
|
||||
|
||||
int ArgumentParser::parseargs(int argc, char** argv) {
|
||||
po::store(po::command_line_parser(argc, argv).options(desc).positional(positionalOptions).run(), vm);
|
||||
po::notify(vm);
|
||||
|
||||
// If help option is specified then output help message
|
||||
if (vm.count("help")) {
|
||||
cout << desc << "\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
po::notify(vm);
|
||||
|
||||
if (vm["analyses"].empty()) {
|
||||
throw runtime_error("No analysis strings provided as arguments.");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
const po::variable_value& ArgumentParser::operator [](char const *b) const {
|
||||
return vm[b];
|
||||
}
|
||||
|
||||
po::variables_map::size_type ArgumentParser::count(char const *ref) {
|
||||
return vm.count(ref);
|
||||
}
|
||||
|
||||
+12
-7
@@ -1,25 +1,30 @@
|
||||
#include <string>
|
||||
#include <list>
|
||||
#include <vector>
|
||||
#include "AudioDatabase.h"
|
||||
#include <stdexcept>
|
||||
#include <set>
|
||||
#include <algorithm>
|
||||
|
||||
using namespace std;
|
||||
|
||||
AudioDatabase::AudioDatabase(
|
||||
const string audio_dir,
|
||||
const string database_dir,
|
||||
list<string>& analyses,
|
||||
Logger* log
|
||||
vector<string>& analyses,
|
||||
Logger* log,
|
||||
const string audio_dir
|
||||
)
|
||||
{
|
||||
this->log = log;
|
||||
|
||||
// Remove duplicate strings from list of analyses.
|
||||
analyses.unique();
|
||||
log->info("Database directory: " + database_dir);
|
||||
log->info("Audio directory: " + audio_dir);
|
||||
// Remove duplicate strings from vector of analyses.
|
||||
std::vector<string>::iterator it;
|
||||
it = std::unique (analyses.begin(), analyses.end()); // 10 20 30 20 10 ? ? ? ?
|
||||
analyses.resize(std::distance(analyses.begin(),it));
|
||||
|
||||
// Check that all analysis strings supplied refer to valid analyses.
|
||||
list<string>::const_iterator valid = check_analyses_valid(analyses.begin(), analyses.end());
|
||||
vector<string>::const_iterator valid = check_analyses_valid(analyses.begin(), analyses.end());
|
||||
if(valid != analyses.end()) {
|
||||
string err = "The analysis string supplied to the AudioDatabase constructor is not valid: " + *valid;
|
||||
throw std::runtime_error(err);
|
||||
|
||||
+24
-4
@@ -19,19 +19,38 @@ int main(int argc, char** argv) {
|
||||
// Initialize a logger object to be used for message handeling throughout
|
||||
// the program
|
||||
Logger log = Logger();
|
||||
/*
|
||||
try
|
||||
{
|
||||
*/
|
||||
// Initialize object to parse arguments supplied by user from command
|
||||
// line
|
||||
ArgumentParser argparse = ArgumentParser();
|
||||
ConcatenatorArgParse argparse = ConcatenatorArgParse();
|
||||
// Parse arguments and exit program if specified (through use of --help
|
||||
// or -h flag)
|
||||
if(argparse.parseargs(argc, argv)) {
|
||||
return ERROR_IN_COMMAND_LINE;
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
vector<string> analyses = argparse.get_analyses();
|
||||
|
||||
list<string> analyses = {"BLARGH"};
|
||||
AudioDatabase db = AudioDatabase("./", "./", analyses, &log);
|
||||
// Initialize the source audio database object with arguments provided from the command line.
|
||||
AudioDatabase source_db = AudioDatabase(
|
||||
argparse.get_source_db(),
|
||||
analyses,
|
||||
&log,
|
||||
argparse.get_src_audio_dir()
|
||||
);
|
||||
|
||||
// Initialize the target audio database object with arguments provided from the command line.
|
||||
AudioDatabase target_db = AudioDatabase(
|
||||
argparse.get_target_db(),
|
||||
analyses,
|
||||
&log,
|
||||
argparse.get_tar_audio_dir()
|
||||
);
|
||||
|
||||
/*
|
||||
}
|
||||
catch(std::exception& e)
|
||||
{
|
||||
@@ -41,5 +60,6 @@ int main(int argc, char** argv) {
|
||||
log.error(error);
|
||||
return ERROR_UNHANDLED_EXCEPTION;
|
||||
}
|
||||
*/
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
@@ -69,6 +69,10 @@ Logger::Logger() {
|
||||
|
||||
};
|
||||
|
||||
Logger::~Logger() {
|
||||
Logger::console_sink->flush();
|
||||
Logger::file_sink->flush();
|
||||
}
|
||||
void Logger::trace(std::string str) {
|
||||
BOOST_LOG_SEV(lg, logging::trivial::trace) << str;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user