Add git-based version (closes #12)

This commit is contained in:
rr- 2016-03-16 17:31:37 +01:00
parent a06f337a63
commit 54571e8bc3
5 changed files with 49 additions and 12 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
/build
tests/catch.h
src/version.h

View File

@ -33,6 +33,18 @@ file(GLOB_RECURSE test_headers "${CMAKE_SOURCE_DIR}/tests/*.h")
list(REMOVE_ITEM common_sources "${CMAKE_SOURCE_DIR}/src/main.cc")
list(REMOVE_ITEM test_sources "${CMAKE_SOURCE_DIR}/tests/main.cc")
# -------
# Version
# -------
execute_process(COMMAND git describe --tags --abbrev=0 OUTPUT_VARIABLE VERSION_SHORT OUTPUT_STRIP_TRAILING_WHITESPACE)
execute_process(COMMAND git describe --always --dirty --long --tags OUTPUT_VARIABLE VERSION_LONG OUTPUT_STRIP_TRAILING_WHITESPACE)
if("${VERSION_SHORT}" STREQUAL "")
set(VERSION_SHORT "0.0")
set(VERSION_LONG "?")
endif()
configure_file("${CMAKE_SOURCE_DIR}/src/version.h.in" "${CMAKE_BINARY_DIR}/generated/version.h" @ONLY)
include_directories(${CMAKE_BINARY_DIR}/generated)
# -------------------
# 3rd party libraries
# -------------------

View File

@ -1,7 +1,6 @@
#include <iostream>
#include "options.h"
static const auto version = "2.x";
#include "version.h"
static void show_usage(const bool include_help)
{
@ -29,7 +28,7 @@ static void show_usage(const bool include_help)
" --import set the tags from scratch basing on stanard input\n"
" -e, --edit spawn the $EDITOR and apply --import on the result\n";
std::cout << "opustags v" << version << "\n";
std::cout << "opustags v" << opustags::version_short << "\n";
std::cout << usage;
if (include_help) {
std::cout << "\n";
@ -37,6 +36,11 @@ static void show_usage(const bool include_help)
}
}
static void show_version()
{
std::cout << "opustags v" << opustags::version_long << "\n";
}
int main(int argc, char **argv)
{
if (argc == 1) {
@ -50,6 +54,10 @@ int main(int argc, char **argv)
show_usage(true);
return EXIT_SUCCESS;
}
if (options.show_version) {
show_version();
return EXIT_SUCCESS;
}
std::cout << "Working...\n";
std::cout << "Input path: " << options.path_in << "\n";

View File

@ -156,16 +156,22 @@ Options opustags::parse_args(const int argc, char **argv)
}
}
if (optind == argc || argv[optind] == NULL) {
throw ArgumentError("Missing input path");
std::vector<std::string> stray;
while (optind < argc)
stray.push_back(argv[optind++]);
if (!options.show_help && !options.show_version)
{
if (stray.empty())
throw ArgumentError("Missing input path");
options.path_in = stray.at(0);
if (options.path_in.empty())
throw ArgumentError("Input path cannot be empty");
if (stray.size() > 1)
throw ArgumentError("Extra argument: " + stray.at(1));
}
options.path_in = argv[optind++];
if (options.path_in.empty())
throw ArgumentError("Input path cannot be empty");
if (optind < argc)
throw ArgumentError("Extra argument: " + std::string(argv[optind]));
return options;
}

10
src/version.h.in Normal file
View File

@ -0,0 +1,10 @@
#pragma once
#include <string>
namespace opustags {
static const std::string version_short = "@VERSION_SHORT@";
static const std::string version_long = "@VERSION_LONG@";
}