Skip to content

Commit ecd0c30

Browse files
committed
main: add -W option to chdir into working directory
1 parent 9c5d569 commit ecd0c30

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

src/main.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ sp_process_stream(const std::string& filename, std::istream& is)
8888

8989
//! define identifiers for command line arguments
9090
enum { OPT_HELP, OPT_VERBOSE, OPT_FILETYPE,
91-
OPT_OUTPUT, OPT_CHECK_OUTPUT, OPT_DATABASE, OPT_RANGE };
91+
OPT_OUTPUT, OPT_CHECK_OUTPUT, OPT_DATABASE, OPT_RANGE,
92+
OPT_WORK_DIR };
9293

9394
//! define command line arguments
9495
static CSimpleOpt::SOption sopt_list[] = {
@@ -100,6 +101,7 @@ static CSimpleOpt::SOption sopt_list[] = {
100101
{ OPT_CHECK_OUTPUT, "-C", SO_NONE },
101102
{ OPT_DATABASE, "-D", SO_REQ_SEP },
102103
{ OPT_RANGE, "-R", SO_REQ_SEP },
104+
{ OPT_WORK_DIR, "-W", SO_REQ_SEP },
103105
SO_END_OF_OPTIONS
104106
};
105107

@@ -116,7 +118,8 @@ sp_process_usage(const std::string& progname)
116118
" -o <file> Output all processed files to this stream." << std::endl <<
117119
" -C Verify that -o output file matches processed data (for tests)." << std::endl <<
118120
" -D <type> Select SQL database type and file or database." << std::endl <<
119-
" -R <name> Process only named RANGE in files." << std::endl);
121+
" -R <name> Process only named RANGE in files." << std::endl <<
122+
" -W <dir> Change working directory at start-up." << std::endl);
120123

121124
return EXIT_FAILURE;
122125
}
@@ -131,6 +134,9 @@ sp_process(int argc, char* argv[])
131134
// database connection to establish
132135
std::string opt_db_conninfo;
133136

137+
// working directory
138+
std::string opt_work_dir;
139+
134140
//! parse command line parameters using SimpleOpt
135141
CSimpleOpt args(argc, argv, sopt_list);
136142

@@ -169,9 +175,18 @@ sp_process(int argc, char* argv[])
169175
case OPT_RANGE:
170176
gopt_ranges.push_back(args.OptionArg());
171177
break;
178+
179+
case OPT_WORK_DIR:
180+
opt_work_dir = args.OptionArg();
181+
break;
172182
}
173183
}
174184

185+
if (!opt_work_dir.empty()) {
186+
if (chdir(opt_work_dir.c_str()) != 0)
187+
OUT_THROW("Error chdir() to work directory: " << strerror(errno));
188+
}
189+
175190
// make connection to the database
176191
if (!g_db_connect(opt_db_conninfo))
177192
OUT_THROW("Fatal: could not connect to a SQL database");

tests/gnuplot/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ foreach(infile ${plot_files})
3131
get_filename_component(basename ${infile} NAME)
3232
# write test case
3333
add_test(NAME gnuplot_${basename}
34-
COMMAND ${CMAKE_BINARY_DIR}/src/sqlplot-tools ${TEST_OPTIONS} ${infile} -o ${outfile}
35-
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
34+
COMMAND ${CMAKE_BINARY_DIR}/src/sqlplot-tools
35+
${TEST_OPTIONS} ${infile} -o ${outfile} -W ${CMAKE_CURRENT_SOURCE_DIR}
3636
)
3737
endforeach()

tests/latex/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ foreach(infile ${tex_files})
3131
get_filename_component(basename ${infile} NAME)
3232
# write test case
3333
add_test(NAME latex_${basename}
34-
COMMAND ${CMAKE_BINARY_DIR}/src/sqlplot-tools ${TEST_OPTIONS} ${infile} -o ${outfile}
35-
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
34+
COMMAND ${CMAKE_BINARY_DIR}/src/sqlplot-tools
35+
${TEST_OPTIONS} ${infile} -o ${outfile} -W ${CMAKE_CURRENT_SOURCE_DIR}
3636
)
3737
endforeach()

0 commit comments

Comments
 (0)