Add support for command line parameters

Using tclap library, released under MIT. This adds the meson
wrap file along with the minimal custom meson.build for tclap,
all the meson code to get config files generated and the
integration of the tclap parser.
This commit is contained in:
King_DuckZ 2025-04-08 02:04:52 +01:00
parent 3f10b30efc
commit 42a4fbc686
9 changed files with 185 additions and 5 deletions

2
.gitignore vendored
View file

@ -1,3 +1,5 @@
tags tags
subprojects/libstriezel subprojects/libstriezel
compile_commands.json compile_commands.json
subprojects/tclap-1.4.0-rc2/
subprojects/packagecache/

View file

@ -11,9 +11,11 @@ project('ducktorrent', 'cpp',
) )
boost_dep = dependency('boost', version: '>=1.75.0') boost_dep = dependency('boost', version: '>=1.75.0')
libstriezel_dep = dependency('libstriezel') libstriezel_dep = dependency('libstriezel')
template_incl = include_directories('templates')
subdir('templates')
subdir('src') subdir('src')
if get_option('build_testing') if get_option('build_testing')

View file

@ -16,6 +16,8 @@
*/ */
#include "ducktorrent/torrent_read.hpp" #include "ducktorrent/torrent_read.hpp"
#include "ducktorrent_config.h"
#include "git_version.h"
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
@ -25,8 +27,31 @@
#include <sha1/sha1.hpp> #include <sha1/sha1.hpp>
#include <sha1/BufferSource.hpp> #include <sha1/BufferSource.hpp>
#include <climits> #include <climits>
#include <tclap/CmdLine.h>
namespace { namespace {
struct CmdOutPrinter : TCLAP::StdOutput {
void version (TCLAP::CmdLineInterface& c) override {
using duck::g_project_name;
using duck::g_project_version;
using duck::g_proj_git_sha1;
std::cout << '\n';
std::cout << g_project_name << " Copyright (C) 2025 Michele \"King_DuckZ\" Santullo\n" <<
"This program comes with ABSOLUTELY NO WARRANTY;\n" <<
"This is free software, and you are welcome to redistribute it\n" <<
"under certain conditions; see the source for copying conditions.\n";
std::cout << '\n' << g_project_name << " version: " <<
g_project_version << '\n';
if constexpr (g_proj_git_sha1[0] != '\0') {
std::cout << "build commit: " << g_proj_git_sha1 << '\n';
}
std::cout << '\n';
}
};
SHA1::MessageDigest to_hash160_digest (const std::array<std::uint32_t, 5>& arr) { SHA1::MessageDigest to_hash160_digest (const std::array<std::uint32_t, 5>& arr) {
SHA1::MessageDigest hash_found; SHA1::MessageDigest hash_found;
std::copy_n(arr.begin(), 5, hash_found.hash); std::copy_n(arr.begin(), 5, hash_found.hash);
@ -35,13 +60,34 @@ SHA1::MessageDigest to_hash160_digest (const std::array<std::uint32_t, 5>& arr)
} //unnamed namespace } //unnamed namespace
int main(int argc, const char* argv[]) { int main(int argc, const char* argv[]) {
if (argc != 2) { std::string torrent_path;
std::cerr << "Wrong number of parameters. Usage:\n"
<< argv[0] << " <path_to_torrent>\n"; try {
CmdOutPrinter custom_printer;
TCLAP::CmdLine cmd("How to use " PROJECT_NAME, ' ', duck::g_project_version);
cmd.setOutput(&custom_printer);
TCLAP::ValueArg<std::string> dir_par("p", "path",
"Path to the location where torrent files are located",
false, "", "path");
cmd.add(dir_par);
TCLAP::UnlabeledValueArg<std::string> torrent_par("torrent_file",
"Path of the torrent file to load",
true, "", "path", false);
cmd.add(torrent_par);
cmd.parse(argc, argv);
torrent_path = torrent_par.getValue();
}
catch (TCLAP::ArgException& e) {
std::cerr << "Error: " << e.error() << " for parameter " << e.argId() << std::endl;
return 2; return 2;
} }
duck::TorrentRead torrent(argv[1], ""); duck::TorrentRead torrent(torrent_path, "");
std::cout << "Loaded file into string of size " << torrent.raw_data_size() << '\n'; std::cout << "Loaded file into string of size " << torrent.raw_data_size() << '\n';
std::cout << "Torrent name: " << torrent.read_name() << '\n'; std::cout << "Torrent name: " << torrent.read_name() << '\n';

View file

@ -1,9 +1,17 @@
tclap_dep = dependency('tclap', version: '>=1.4.0')
executable(meson.project_name(), executable(meson.project_name(),
'main.cpp', 'main.cpp',
gitrev_config_file,
misc_config_file,
dependencies: [ dependencies: [
boost_dep, boost_dep,
libstriezel_dep, libstriezel_dep,
ducktorrent_dep, ducktorrent_dep,
tclap_dep,
], ],
install: true, install: true,
include_directories: [
template_incl
],
) )

View file

@ -0,0 +1,16 @@
project('tclap', 'cpp',
version: '1.4.0',
meson_version: '>=0.55.0',
default_options: [
'buildtype=release',
'cpp_std=gnu++11',
'b_ndebug=if-release',
],
license: 'MIT',
)
pub_inc = include_directories('include')
tclap_dep = declare_dependency(
include_directories: pub_inc,
)

11
subprojects/tclap.wrap Normal file
View file

@ -0,0 +1,11 @@
[wrap-file]
directory = tclap-1.4.0-rc2
source_url = https://downloads.sourceforge.net/project/tclap/tclap-1.4.0-rc2.tar.bz2
source_filename = tclap-1.4.0-rc2.tar.bz2
source_hash = ca52ce5badc477aeda59866601aad85c55e014c5400c15ed13e21fe7d0c1c5f7
patch_directory = tclap
[provide]
dependency_names = tclap-1.4.0-rc2
tclap = tclap_dep

View file

@ -0,0 +1,34 @@
/* Copyright 2025, Michele "King_DuckZ" Santullo
* This file is part of ducktorrent.
*
* Ducktorrent is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Ducktorrent is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ducktorrent. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#if defined(__cplusplus)
namespace duck {
constexpr char g_project_name[] = @PROJECT_NAME@;
constexpr char g_project_version[] = "@PROJECT_VERSION_MAJOR@.@PROJECT_VERSION_MINOR@.@PROJECT_VERSION_PATCH@";
constexpr unsigned int g_proj_ver_major = @PROJECT_VERSION_MAJOR@;
constexpr unsigned int g_proj_ver_minor = @PROJECT_VERSION_MINOR@;
constexpr unsigned int g_proj_ver_patch = @PROJECT_VERSION_PATCH@;
} //namespace duck
#endif
#define PROJECT_NAME @PROJECT_NAME@
#define PROJECT_VERSION "@PROJECT_VERSION_MAJOR@.@PROJECT_VERSION_MINOR@.@PROJECT_VERSION_PATCH@"
#define PROJ_VER_MAJOR @PROJECT_VERSION_MAJOR@
#define PROJ_VER_MINOR @PROJECT_VERSION_MINOR@
#define PROJ_VER_PATCH @PROJECT_VERSION_PATCH@

View file

@ -0,0 +1,26 @@
/* Copyright 2025, Michele "King_DuckZ" Santullo
* This file is part of ducktorrent.
*
* Ducktorrent is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Ducktorrent is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ducktorrent. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#if defined(__cplusplus)
namespace duck {
constexpr char g_proj_git_sha1[] = "@VCS_TAG@";
} //namespace duck
#endif
#define PROJ_GIT_SHA1 "@VCS_TAG@"

35
templates/meson.build Normal file
View file

@ -0,0 +1,35 @@
git_prog = find_program('git', required: false)
conf_data = configuration_data()
conf_data.set_quoted('PROJECT_ROOT', meson.project_source_root())
conf_data.set_quoted('PROJECT_NAME', meson.project_name())
version_arr = meson.project_version().split('.')
conf_data.set('PROJECT_VERSION_MAJOR', version_arr[0])
conf_data.set('PROJECT_VERSION_MINOR', version_arr[1])
conf_data.set('PROJECT_VERSION_PATCH', version_arr[2])
if git_prog.found()
gitrev_config_file = vcs_tag(
command: [git_prog.path(), 'rev-parse', 'HEAD'],
input: 'git_version.h.in',
output: 'git_version.h',
fallback: ''
)
else
conf = configuration_data()
conf.set('VCS_TAG', '')
gitrev_config_file = configure_file(
input: 'git_version.h.in',
output: 'git_version.h',
configuration: conf
)
unset_variable('conf')
endif
misc_config_file = configure_file(
input: meson.project_name() + '_config.h.in',
output: meson.project_name() + '_config.h',
configuration: conf_data
)
unset_variable('conf_data')
unset_variable('version_arr')