Skip to content

Commit 738cffe

Browse files
committed
(wip) feature: add edit subcommand (#20)
1 parent defbfb6 commit 738cffe

File tree

4 files changed

+36
-0
lines changed

4 files changed

+36
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ Here is a short introduction how to use **booktrack-cli**.
4040

4141
# Delete a book from the given library by the book id
4242
./booktrack-cli -l ~/my_lib.json delete --by-id 2
43+
44+
# Edit the book from the given library with the book id 4
45+
./booktrack-cli -l ~/my_lib.json edit --by-id 4
4346
```
4447

4548
You can see a full list of commandline options below.
@@ -59,6 +62,7 @@ Subcommands:
5962
list List books in the library
6063
statistics Show statistics for loaded library
6164
details Show details for a book
65+
edit Edit a book in the library
6266
```
6367

6468
## Notes

src/cli.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,14 @@ void AddDetailsOptions(CLI::App& app, CliOptions& opt) {
9191
sub_details->add_option("--by-id", opt.details.id, "")->mandatory();
9292
}
9393

94+
void AddEditOptions(CLI::App& app, CliOptions& opt) {
95+
auto sub_edit =
96+
app.add_subcommand("edit", "Edit a book in the library")
97+
->callback([&opt]() { opt.command = PrimaryCommand::kEdit; });
98+
99+
sub_edit->add_option("--by-id", opt.edit.id, "")->mandatory();
100+
}
101+
94102
} // namespace
95103

96104
void booktrack_cli::AddCliOptions(CLI::App& app, CliOptions& opt) {
@@ -100,4 +108,5 @@ void booktrack_cli::AddCliOptions(CLI::App& app, CliOptions& opt) {
100108
AddListOptions(app, opt);
101109
AddStatisticsOptions(app, opt);
102110
AddDetailsOptions(app, opt);
111+
AddEditOptions(app, opt);
103112
}

src/cli.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ enum class PrimaryCommand {
1616
kDelete,
1717
kStatistics,
1818
kDetails,
19+
kEdit,
1920
};
2021

2122
/**
@@ -59,6 +60,13 @@ struct CliOptionsDetails {
5960
size_t id{0};
6061
};
6162

63+
/**
64+
* @brief Contains available options for the "edit" subcommand
65+
*/
66+
struct CliOptionsEdit {
67+
size_t id{0};
68+
};
69+
6270
/**
6371
* @brief Structure containing all options for the command line interface
6472
*/
@@ -70,6 +78,7 @@ struct CliOptions {
7078
CliOptionsList list;
7179
CliOptionsStatistics statistics;
7280
CliOptionsDetails details;
81+
CliOptionsEdit edit;
7382
};
7483

7584
/**

src/main.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,17 @@ void SubcmdDetails(Library& lib, const CliOptionsDetails& options) {
9393
}
9494
}
9595

96+
void SubcmdEdit(Library& lib, [[maybe_unused]] const CliOptionsEdit& options) {
97+
// TODO: implement book editing
98+
auto check_exist = lib.GetBookById(options.id);
99+
if (check_exist.has_value()) {
100+
auto book = check_exist.value();
101+
std::cout << "Edit " << book.GetTitle() << '\n';
102+
} else {
103+
std::cout << "Could not find a book with id: " << options.id << ".\n";
104+
}
105+
}
106+
96107
} // namespace
97108

98109
int main(int argc, char* argv[]) {
@@ -120,6 +131,9 @@ int main(int argc, char* argv[]) {
120131
case PrimaryCommand::kDetails:
121132
SubcmdDetails(library_from_file, options.details);
122133
break;
134+
case PrimaryCommand::kEdit:
135+
SubcmdEdit(library_from_file, options.edit);
136+
break;
123137
default:
124138
break;
125139
}

0 commit comments

Comments
 (0)