Skip to content

Commit

Permalink
Merge pull request #217 from poacpm/add/init-cmd
Browse files Browse the repository at this point in the history
Add init command
  • Loading branch information
ken-matsui authored Nov 30, 2020
2 parents 95c8676 + 13427ac commit 1c2c941
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 64 deletions.
1 change: 1 addition & 0 deletions include/poac/cmd.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef POAC_CMD_HPP
#define POAC_CMD_HPP

#include <poac/cmd/init.hpp>
#include <poac/cmd/version.hpp>

#endif // !POAC_CMD_HPP
37 changes: 7 additions & 30 deletions include/poac/cmd/init.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#ifndef POAC_OPTS_INIT_HPP
#define POAC_OPTS_INIT_HPP
#ifndef POAC_CMD_INIT_HPP
#define POAC_CMD_INIT_HPP

// Std
#include <future>
#include <iostream>
#include <fstream>
#include <string>
Expand All @@ -20,13 +19,6 @@
#include <poac/util/termcolor2/termcolor2.hpp>

namespace poac::cmd::init {
inline const clap::subcommand cli =
clap::subcommand("init")
.about("Create a new poac package in an existing directory")
.arg(clap::opt("bin", "Use a binary (application) template [default]"))
.arg(clap::opt("lib", "Use a library template"))
;

struct Options {
_new::ProjectType type;
};
Expand Down Expand Up @@ -54,29 +46,14 @@ namespace poac::cmd::init {
}

[[nodiscard]] std::optional<core::except::Error>
exec(std::future<std::optional<io::config::Config>>&&, std::vector<std::string>&& args) {
if (args.size() > 1) {
return core::except::Error::InvalidSecondArg::Init;
} else if (io::config::detail::validate_config()) {
return core::except::Error::General{
"`poac init` cannot be run on existing poac packages"
};
}

init::Options opts{};
const bool bin = util::argparse::use_rm(args, "-b", "--bin");
const bool lib = util::argparse::use_rm(args, "-l", "--lib");
if (bin && lib) {
exec(Options&& opts) {
if (io::config::detail::validate_config()) {
return core::except::Error::General{
"You cannot specify both lib and binary outputs."
"`poac init` cannot run on existing poac packages"
};
} else if (!bin && lib) {
opts.type = _new::ProjectType::Lib;
} else {
opts.type = _new::ProjectType::Bin;
}
return init::init(std::move(opts));
return init(std::forward<Options>(opts));
}
} // end namespace

#endif // !POAC_OPTS_INIT_HPP
#endif // !POAC_CMD_INIT_HPP
89 changes: 55 additions & 34 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

enum class subcommand {
nothing,
init,
help,
version,
};
Expand All @@ -26,7 +27,7 @@ no_such_command(const int& argc, char* argv[], const clipp::group& cli) {
std::cerr
<< fmt::format(
"{}: no such command: `{}`",
termcolor2::to_red(termcolor2::to_bold(std::string("error"))),
termcolor2::to_red(termcolor2::to_bold(std::string("Error"))),
fmt::join(argv + 1, argv + argc," ")
)
<< std::endl
Expand All @@ -39,55 +40,75 @@ no_such_command(const int& argc, char* argv[], const clipp::group& cli) {
template <typename T>
int
optional_to_int(const std::optional<T>& opt) noexcept {
return opt.has_value() ? EXIT_FAILURE : EXIT_SUCCESS;
}

[[nodiscard]] std::optional<poac::core::except::Error>
exec(const subcommand& subcmd, const clipp::group& cli) {
switch (subcmd) {
case subcommand::help:
std::cout << clipp::make_man_page(cli, "poac");
return std::nullopt;
case subcommand::version:
return poac::cmd::version::exec();
case subcommand::nothing:
return std::nullopt;
}
return opt.has_value()
? (std::cerr << opt->what() << std::endl, EXIT_FAILURE)
: EXIT_SUCCESS;
}

int
main(const int argc, char* argv[]) {
subcommand subcmd = subcommand::nothing;
option opt = option::nothing;

auto init_opts = poac::cmd::init::Options {
poac::cmd::_new::ProjectType::Bin
};
const clipp::group init_cmd =
( clipp::command("init")
.set(subcmd, subcommand::init)
.doc("Create a new poac package in an existing directory")
, ( clipp::option("--bin", "-b")
.doc("Use a binary (application) template [default]")
| clipp::option("--lib", "-l")
.set(init_opts.type, poac::cmd::_new::ProjectType::Lib)
.doc("Use a library template")
)
);

const clipp::parameter help_cmd =
clipp::command("help")
.set(subcmd, subcommand::help)
.doc("Print this message");

const clipp::parameter version_cmd =
clipp::command("version")
.set(subcmd, subcommand::version)
.doc("Show the current poac version");

const clipp::group cli = (
clipp::with_prefixes_short_long(
"-",
"--",
clipp::option("V", "version")
.set(subcmd, subcommand::version)
.doc("Print version info"),
clipp::option("v", "verbose")
.set(opt, option::verbose)
.doc("Use verbose output"),
clipp::option("q", "quiet")
.set(opt, option::quiet)
.doc("No output printed to stdout")
( clipp::option("--help", "-h")
.set(subcmd, subcommand::help)
.doc("Print this message or the help of the given subcommand(s)")
, clipp::option("--version", "-V")
.set(subcmd, subcommand::version)
.doc("Show the current poac version")
, clipp::option("--verbose", "-v")
.set(opt, option::verbose)
.doc("Use verbose output")
, clipp::option("--quiet", "-q")
.set(opt, option::quiet)
.doc("No output printed to stdout")
) |
(
clipp::command("help").set(subcmd, subcommand::help) |
clipp::command("version").set(subcmd, subcommand::version)
( init_cmd
| help_cmd
| version_cmd
)
);

if (argc == 1) {
std::cout << clipp::usage_lines(cli, "poac") << std::endl;
return EXIT_SUCCESS;
} else if (clipp::parse(argc, argv, cli)) {
if (subcmd == subcommand::nothing) {
return no_such_command(argc, argv, cli);
} else {
return optional_to_int(exec(subcmd, cli));
switch (subcmd) {
case subcommand::nothing:
return no_such_command(argc, argv, cli);
case subcommand::init:
return optional_to_int(poac::cmd::init::exec(std::move(init_opts)));
case subcommand::help:
std::cout << clipp::make_man_page(cli, "poac");
return EXIT_SUCCESS;
case subcommand::version:
return optional_to_int(poac::cmd::version::exec());
}
} else {
return no_such_command(argc, argv, cli);
Expand Down

0 comments on commit 1c2c941

Please sign in to comment.