Skip to content

Commit

Permalink
Tweaks to internal command initialisation
Browse files Browse the repository at this point in the history
- New function verify_usage() performs AUTHOR and SYNOPSIS non-empty check.
- Remove default authorship from R command initialisation.
- Remove DEBUG() call within ProgressBar::set_update_method(), since App::log_level will not yet be set.
  • Loading branch information
Lestropie committed Aug 24, 2017
1 parent d3d2e28 commit 580ce27
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 100 deletions.
42 changes: 23 additions & 19 deletions core/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -888,35 +888,39 @@ namespace MR



void parse_special_options ()
void verify_usage ()
{
if (!AUTHOR)
throw Exception ("No author specified for command " + std::string(NAME));
if (!SYNOPSIS)
throw Exception ("No synopsis specified for command " + std::string(NAME));
}


if (argc == 2) {
if (strcmp (argv[1], "__print_full_usage__") == 0) {
print (full_usage ());
throw 0;
}
if (strcmp (argv[1], "__print_usage_markdown__") == 0) {
print (markdown_usage ());
throw 0;
}
if (strcmp (argv[1], "__print_usage_rst__") == 0) {
print (restructured_text_usage ());
throw 0;
}
if (strcmp (argv[1], "__print_synopsis__") == 0) {
print (SYNOPSIS);
throw 0;
}
}

void parse_special_options ()
{
if (argc != 2) return;
if (strcmp (argv[1], "__print_full_usage__") == 0) {
print (full_usage ());
throw 0;
}
if (strcmp (argv[1], "__print_usage_markdown__") == 0) {
print (markdown_usage ());
throw 0;
}
if (strcmp (argv[1], "__print_usage_rst__") == 0) {
print (restructured_text_usage ());
throw 0;
}
if (strcmp (argv[1], "__print_synopsis__") == 0) {
print (SYNOPSIS);
throw 0;
}
}



void parse ()
{
argument.clear();
Expand Down
21 changes: 12 additions & 9 deletions core/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ namespace MR
std::string help_synopsis (int format);
std::string help_tail (int format);
std::string usage_syntax (int format);




Expand Down Expand Up @@ -128,12 +128,12 @@ namespace MR



inline void check_overwrite (const std::string& name)
inline void check_overwrite (const std::string& name)
{
if (Path::exists (name) && !overwrite_files) {
if (check_overwrite_files_func)
check_overwrite_files_func (name);
else
else
throw Exception ("output file \"" + name + "\" already exists (use -force option to force overwrite)");
}
}
Expand All @@ -147,6 +147,9 @@ namespace MR
* processing takes place. */
void init (int argc, const char* const* argv);

//! verify that command's usage() function has set requisite fields [used internally]
void verify_usage ();

//! option parsing that should happen before GUI creation [used internally]
void parse_special_options ();

Expand Down Expand Up @@ -269,7 +272,7 @@ namespace MR
* a paragraph to the description using the '+' operator, e.g.:
* \code
* void usage() {
* DESCRIPTION
* DESCRIPTION
* + "This command can be used in lots of ways "
* "and is very versatile."
*
Expand All @@ -292,7 +295,7 @@ namespace MR
* ARGUMENTS
* + Argument ("in", "the input image").type_image_in()
* + Argument ("factor", "the factor to use in the analysis").type_float()
* + Argument ("out", "the output image").type_image_out();
* + Argument ("out", "the output image").type_image_out();
* }
* \endcode
*/
Expand Down Expand Up @@ -361,12 +364,12 @@ namespace MR
* }
* \endcode */
const vector<ParsedOption> get_options (const std::string& name);


//! Returns the option value if set, and the default otherwise.
/*! Returns the value of (the first occurence of) option \c name
* or the default value provided as second argument.
*
*
* Use:
* \code
* float arg1 = get_option_value("myopt", arg1_default);
Expand All @@ -380,7 +383,7 @@ namespace MR
T r = (opt.size()) ? opt[0][0] : default_value;
return r;
}


//! convenience function provided mostly to ease writing Exception strings
inline std::string operator+ (const char* left, const App::ParsedArgument& right)
Expand Down
87 changes: 44 additions & 43 deletions core/command.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,46 +24,46 @@

#ifdef MRTRIX_AS_R_LIBRARY

extern "C" void R_main (int* cmdline_argc, char** cmdline_argv)
{
::MR::App::build_date = __DATE__;
extern "C" void R_main (int* cmdline_argc, char** cmdline_argv)
{
::MR::App::build_date = __DATE__;
#ifdef MRTRIX_PROJECT_VERSION
::MR::App::project_version = MRTRIX_PROJECT_VERSION;
#endif
SET_MRTRIX_PROJECT_VERSION
::MR::App::AUTHOR = "J-Donald Tournier ([email protected])";
::MR::App::DESCRIPTION.clear();
::MR::App::ARGUMENTS.clear();
::MR::App::OPTIONS.clear();
try {
usage();
::MR::App::init (*cmdline_argc, cmdline_argv);
::MR::App::parse ();
run ();
}
catch (MR::Exception& E) {
E.display();
return;
}
catch (int retval) {
return;
}
}
SET_MRTRIX_PROJECT_VERSION
::MR::App::DESCRIPTION.clear();
::MR::App::ARGUMENTS.clear();
::MR::App::OPTIONS.clear();
try {
usage();
::MR::App::verify_usage();
::MR::App::init (*cmdline_argc, cmdline_argv);
::MR::App::parse ();
run ();
}
catch (MR::Exception& E) {
E.display();
return;
}
catch (int retval) {
return;
}
}

extern "C" void R_usage (char** output)
{
::MR::App::DESCRIPTION.clear();
::MR::App::ARGUMENTS.clear();
::MR::App::OPTIONS.clear();
usage();
std::string s = MR::App::full_usage();
*output = new char [s.size()+1];
strncpy(*output, s.c_str(), s.size()+1);
extern "C" void R_usage (char** output)
{
::MR::App::DESCRIPTION.clear();
::MR::App::ARGUMENTS.clear();
::MR::App::OPTIONS.clear();
usage();
std::string s = MR::App::full_usage();
*output = new char [s.size()+1];
strncpy(*output, s.c_str(), s.size()+1);
}

#else

int main (int cmdline_argc, char** cmdline_argv)
int main (int cmdline_argc, char** cmdline_argv)
{
#ifdef FLUSH_TO_ZERO
// use gcc switches: -msse -mfpmath=sse -ffast-math
Expand All @@ -74,28 +74,29 @@ int main (int cmdline_argc, char** cmdline_argv)
mxcsr |= (1<<6); // denormals-are-zero
_mm_setcsr (mxcsr);
#endif
::MR::App::build_date = __DATE__;
::MR::App::build_date = __DATE__;
#ifdef MRTRIX_PROJECT_VERSION
::MR::App::project_version = MRTRIX_PROJECT_VERSION;
#endif
try {
::MR::App::init (cmdline_argc, cmdline_argv);
usage ();
::MR::App::init (cmdline_argc, cmdline_argv);
usage ();
::MR::App::verify_usage();
::MR::App::parse_special_options();
#ifdef __gui_app_h__
::MR::GUI::App app (cmdline_argc, cmdline_argv);
#endif
::MR::App::parse ();
::MR::App::parse ();
run ();
}
}
catch (::MR::Exception& E) {
E.display();
E.display();
return 1;
}
catch (int retval) {
return retval;
}
return 0;
}
catch (int retval) {
return retval;
}
return 0;
}

#endif
Expand Down
15 changes: 7 additions & 8 deletions core/exception.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,17 @@

namespace MR
{
namespace App
namespace App
{
extern int log_level;
extern std::string NAME;
}

//! print primary output to stdout as-is.
//! print primary output to stdout as-is.
/*! This function is intended for cases where the command's primary output is text, not
* image data, etc. It is \e not designed for error or status reports: it
* prints to stdout, whereas all reporting functions print to stderr. This is
* to allow the output of the command to be used directly in text processing
* pipeline or redirected to file.
* pipeline or redirected to file.
* \note the use of stdout is normally reserved for piping data files (or at
* least their filenames) between MRtrix commands. This function should
* therefore never be used in commands that produce output images, as the two
Expand All @@ -48,10 +47,10 @@ namespace MR


//! \cond skip

// for internal use only
inline void __print_stderr (const std::string& text)

inline void __print_stderr (const std::string& text)
{
#ifdef MRTRIX_AS_R_LIBRARY
REprintf (text.c_str());
Expand All @@ -61,7 +60,7 @@ namespace MR
}
//! \endcond

//! display error, warning, debug, etc. message to user
//! display error, warning, debug, etc. message to user
/*! types are: 0: error; 1: warning; 2: additional information; 3:
* debugging information; anything else: none. */
extern void (*report_to_user_func) (const std::string& msg, int type);
Expand Down
33 changes: 16 additions & 17 deletions core/progressbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,22 @@ namespace MR
void display_func_terminal (ProgressInfo& p)
{
__need_newline = true;
if (p.multiplier)
__print_stderr (printf ("\r%s: [%3zu%%] %s%s" CLEAR_LINE_CODE,
if (p.multiplier)
__print_stderr (printf ("\r%s: [%3zu%%] %s%s" CLEAR_LINE_CODE,
App::NAME.c_str(), p.value, p.text.c_str(), p.ellipsis.c_str()));
else
__print_stderr (printf ("\r%s: [%s] %s%s" CLEAR_LINE_CODE,
__print_stderr (printf ("\r%s: [%s] %s%s" CLEAR_LINE_CODE,
App::NAME.c_str(), busy[p.value%6], p.text.c_str(), p.ellipsis.c_str()));
}


void done_func_terminal (ProgressInfo& p)
{
if (p.multiplier)
__print_stderr (printf ("\r%s: [100%%] %s" CLEAR_LINE_CODE "\n",
__print_stderr (printf ("\r%s: [100%%] %s" CLEAR_LINE_CODE "\n",
App::NAME.c_str(), p.text.c_str()));
else
__print_stderr (printf ("\r%s: [done] %s" CLEAR_LINE_CODE "\n",
__print_stderr (printf ("\r%s: [done] %s" CLEAR_LINE_CODE "\n",
App::NAME.c_str(), p.text.c_str()));
__need_newline = false;
}
Expand All @@ -77,16 +77,16 @@ namespace MR
count = next_update_at = 0;
if (count++ == next_update_at) {
if (p.multiplier) {
__print_stderr (printf ("%s: [%3zu%%] %s%s\n",
__print_stderr (printf ("%s: [%3zu%%] %s%s\n",
App::NAME.c_str(), p.value, p.text.c_str(), p.ellipsis.c_str()));;
}
else {
__print_stderr (printf ("%s: [%s] %s%s\n",
__print_stderr (printf ("%s: [%s] %s%s\n",
App::NAME.c_str(), busy[p.value%6], p.text.c_str(), p.ellipsis.c_str()));
}
if (next_update_at)
next_update_at *= 2;
else
next_update_at *= 2;
else
next_update_at = 1;
}
}
Expand All @@ -95,7 +95,7 @@ namespace MR
__need_newline = true;
if (p.multiplier) {
if (p.value == 0) {
__print_stderr (printf ("%s: %s%s [",
__print_stderr (printf ("%s: %s%s [",
App::NAME.c_str(), p.text.c_str(), p.ellipsis.c_str()));;
}
else if (p.value%2 == 0) {
Expand All @@ -104,7 +104,7 @@ namespace MR
}
else {
if (p.value == 0) {
__print_stderr (printf ("%s: %s%s ",
__print_stderr (printf ("%s: %s%s ",
App::NAME.c_str(), p.text.c_str(), p.ellipsis.c_str()));;
}
else if (!(p.value & (p.value-1))) {
Expand All @@ -126,7 +126,7 @@ namespace MR
}
}
else {
if (p.multiplier)
if (p.multiplier)
__print_stderr (printf ("]\n"));
else
__print_stderr (printf (" done\n"));
Expand All @@ -145,16 +145,15 @@ namespace MR



bool ProgressBar::set_update_method ()
bool ProgressBar::set_update_method ()
{
bool stderr_to_file = false;

struct stat buf;
if (fstat (STDERR_FILENO, &buf)) {
DEBUG ("Unable to determine nature of stderr; assuming socket");
if (fstat (STDERR_FILENO, &buf))
// unable to determine nature of stderr; assuming socket
stderr_to_file = false;
}
else
else
stderr_to_file = S_ISREG (buf.st_mode);


Expand Down
Loading

0 comments on commit 580ce27

Please sign in to comment.