comparison OrthancServer/main.cpp @ 1666:d7039be97eeb

better command-line options handling
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 30 Sep 2015 14:04:25 +0200
parents 2e692c83e2f3
children de1413733c97
comparison
equal deleted inserted replaced
1662:09be34b2f30e 1666:d7039be97eeb
391 { 391 {
392 std::cout 392 std::cout
393 << "Usage: " << path << " [OPTION]... [CONFIGURATION]" << std::endl 393 << "Usage: " << path << " [OPTION]... [CONFIGURATION]" << std::endl
394 << "Orthanc, lightweight, RESTful DICOM server for healthcare and medical research." << std::endl 394 << "Orthanc, lightweight, RESTful DICOM server for healthcare and medical research." << std::endl
395 << std::endl 395 << std::endl
396 << "If no configuration file is given on the command line, a set of default " << std::endl 396 << "The \"CONFIGURATION\" argument can be a single file or a directory. In the " << std::endl
397 << "case of a directory, all the JSON files it contains will be merged. " << std::endl
398 << "If no configuration path is given on the command line, a set of default " << std::endl
397 << "parameters is used. Please refer to the Orthanc homepage for the full " << std::endl 399 << "parameters is used. Please refer to the Orthanc homepage for the full " << std::endl
398 << "instructions about how to use Orthanc <http://www.orthanc-server.com/>." << std::endl 400 << "instructions about how to use Orthanc <http://www.orthanc-server.com/>." << std::endl
399 << std::endl 401 << std::endl
400 << "Command-line options:" << std::endl 402 << "Command-line options:" << std::endl
401 << " --help\t\tdisplay this help and exit" << std::endl 403 << " --help\t\tdisplay this help and exit" << std::endl
620 if (currentVersion == ORTHANC_DATABASE_VERSION) 622 if (currentVersion == ORTHANC_DATABASE_VERSION)
621 { 623 {
622 return true; 624 return true;
623 } 625 }
624 626
627 if (currentVersion > ORTHANC_DATABASE_VERSION)
628 {
629 LOG(ERROR) << "The version of the database (" << currentVersion
630 << ") is too recent for this version of Orthanc. Please upgrade Orthanc.";
631 return false;
632 }
633
625 if (!allowDatabaseUpgrade) 634 if (!allowDatabaseUpgrade)
626 { 635 {
627 LOG(ERROR) << "The database must be upgraded from version " 636 LOG(ERROR) << "The database must be upgraded from version "
628 << currentVersion << " to " << ORTHANC_DATABASE_VERSION 637 << currentVersion << " to " << ORTHANC_DATABASE_VERSION
629 << ": Please run Orthanc with the \"--upgrade\" command-line option"; 638 << ": Please run Orthanc with the \"--upgrade\" command-line option";
768 int main(int argc, char* argv[]) 777 int main(int argc, char* argv[])
769 { 778 {
770 Logging::Initialize(); 779 Logging::Initialize();
771 780
772 bool allowDatabaseUpgrade = false; 781 bool allowDatabaseUpgrade = false;
782 const char* configurationFile = NULL;
783
784
785 /**
786 * Parse the command-line options.
787 **/
773 788
774 for (int i = 1; i < argc; i++) 789 for (int i = 1; i < argc; i++)
775 { 790 {
776 if (std::string(argv[i]) == "--help") 791 std::string argument(argv[i]);
792
793 if (argument.empty())
794 {
795 // Ignore empty arguments
796 }
797 else if (argument[0] != '-')
798 {
799 if (configurationFile != NULL)
800 {
801 LOG(ERROR) << "More than one configuration path were provided on the command line, aborting";
802 return -1;
803 }
804 else
805 {
806 // Use the first argument that does not start with a "-" as
807 // the configuration file
808 configurationFile = argv[i];
809 }
810 }
811 else if (argument == "--help")
777 { 812 {
778 PrintHelp(argv[0]); 813 PrintHelp(argv[0]);
779 return 0; 814 return 0;
780 } 815 }
781 816 else if (argument == "--version")
782 if (std::string(argv[i]) == "--version")
783 { 817 {
784 PrintVersion(argv[0]); 818 PrintVersion(argv[0]);
785 return 0; 819 return 0;
786 } 820 }
787 821 else if (argument == "--verbose")
788 if (std::string(argv[i]) == "--verbose")
789 { 822 {
790 Logging::EnableInfoLevel(true); 823 Logging::EnableInfoLevel(true);
791 } 824 }
792 825 else if (argument == "--trace")
793 if (std::string(argv[i]) == "--trace")
794 { 826 {
795 Logging::EnableTraceLevel(true); 827 Logging::EnableTraceLevel(true);
796 } 828 }
797 829 else if (boost::starts_with(argument, "--logdir="))
798 if (boost::starts_with(argv[i], "--logdir=")) 830 {
799 { 831 std::string directory = argument.substr(9);
800 std::string directory = std::string(argv[i]).substr(9);
801 832
802 try 833 try
803 { 834 {
804 Logging::SetTargetFolder(directory); 835 Logging::SetTargetFolder(directory);
805 } 836 }
806 catch (OrthancException&) 837 catch (OrthancException&)
807 { 838 {
808 fprintf(stderr, "The directory where to store the log files (%s) is inexistent, aborting.\n", directory.c_str()); 839 LOG(ERROR) << "The directory where to store the log files ("
840 << directory << ") is inexistent, aborting.";
809 return -1; 841 return -1;
810 } 842 }
811 } 843 }
812 844 else if (argument == "--upgrade")
813 if (std::string(argv[i]) == "--upgrade")
814 { 845 {
815 allowDatabaseUpgrade = true; 846 allowDatabaseUpgrade = true;
816 } 847 }
817 848 else if (boost::starts_with(argument, "--config="))
818 if (boost::starts_with(argv[i], "--config="))
819 { 849 {
820 std::string configurationSample; 850 std::string configurationSample;
821 GetFileResource(configurationSample, EmbeddedResources::CONFIGURATION_SAMPLE); 851 GetFileResource(configurationSample, EmbeddedResources::CONFIGURATION_SAMPLE);
822 852
823 #if defined(_WIN32) 853 #if defined(_WIN32)
824 // Replace UNIX newlines with DOS newlines 854 // Replace UNIX newlines with DOS newlines
825 boost::replace_all(configurationSample, "\n", "\r\n"); 855 boost::replace_all(configurationSample, "\n", "\r\n");
826 #endif 856 #endif
827 857
828 std::string target = std::string(argv[i]).substr(9); 858 std::string target = argument.substr(9);
829 std::ofstream f(target.c_str()); 859 Toolbox::WriteFile(configurationSample, target);
830 f << configurationSample;
831 f.close();
832 return 0; 860 return 0;
833 } 861 }
834 } 862 else
835 863 {
836 const char* configurationFile = NULL; 864 LOG(WARNING) << "Option unsupported by the core of Orthanc: " << argument;
837 for (int i = 1; i < argc; i++) 865 }
838 { 866 }
839 // Use the first argument that does not start with a "-" as 867
840 // the configuration file 868
841 if (argv[i][0] != '-') 869 /**
842 { 870 * Launch Orthanc.
843 configurationFile = argv[i]; 871 **/
844 }
845 }
846 872
847 LOG(WARNING) << "Orthanc version: " << ORTHANC_VERSION; 873 LOG(WARNING) << "Orthanc version: " << ORTHANC_VERSION;
848 874
849 int status = 0; 875 int status = 0;
850 try 876 try