Skip to content

Commit 4e74860

Browse files
committed
cstrans-df-run: introduce readListFromValMap() helper
... to eliminate duplicated code. No change in behavior intended with this commit.
1 parent 0d111d3 commit 4e74860

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

src/cstrans-df-run.cc

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,20 @@ typedef std::vector<std::string> TStringList;
3333

3434
const char *prog_name;
3535

36+
/// read non-empty list from value map "vm" with key "key" and store to "dst"
37+
template<class TDst, class TMap>
38+
bool readListFromValMap(TDst *pDst, const TMap &vm, const char *key)
39+
{
40+
if (!vm.count(key))
41+
// "key" not found in "vm"
42+
return false;
43+
44+
// store the list to "*pDst" and return "true" if the list is not empty
45+
TDst &dst = *pDst;
46+
dst = vm[key].template as<TDst>();
47+
return !dst.empty();
48+
}
49+
3650
class DockerFileTransformer {
3751
public:
3852
DockerFileTransformer(const TStringList &prefixCmd, const bool verbose):
@@ -353,13 +367,9 @@ int main(int argc, char *argv[])
353367

354368
const bool verbose = !!vm.count("verbose");
355369

356-
if (!vm.count("prefix-cmd")) {
357-
desc.print(std::cerr);
358-
return 1;
359-
}
360-
361-
const TStringList &prefixCmd = vm["prefix-cmd"].as<TStringList>();
362-
if (prefixCmd.empty()) {
370+
// read the prefix command
371+
TStringList prefixCmd;
372+
if (!readListFromValMap(&prefixCmd, vm, "prefix-cmd")) {
363373
desc.print(std::cerr);
364374
return 1;
365375
}

0 commit comments

Comments
 (0)