Categories
API Articles Blog Coders Codex Development JUCE Linux Development Portfolio Programming

Coders Codex::Command Line Arguments Example for JUCE C++

INTRODUCTION

I base this article around the need to pass command line parameters to a running program. Please note this does not seem to work with Windows yet (working in Linux and OSX not tested).

DISCUSSION

The idea is to use a set of command line parameters to execute a set of built in software tests between two or more instances of a given software and then automatically generate unit test reports. The whole thing can be automated via the sequencing of various tests in a bash script or batch file.

//============================================================================
// Name        : Command Line Arguments
// Author      : Divide To Infinity
// Version     : V1.0
// Copyleft    : Divide To Infinity 2015 - GNU GPL v2
// Description : JUCE Command Line Arguments Demo C++11, Ansi-style
//============================================================================

#include "../JuceLibraryCode/JuceHeader.h"

class CommandLineArguments : public JUCEApplication
{
    public:
        CommandLineArguments(){}
        ~CommandLineArguments(){}

        void initialise(const String& commandLine) override
        {
            Logger * log = Logger::getCurrentLogger();
            log->writeToLog(getApplicationName());

            // Now list the command line arguments
            log->writeToLog("See the entered command line parameters below: ");
            log->writeToLog(JUCEApplicationBase::getCommandLineParameters());
        }

        void shutdown() override {}

        const String getApplicationName() override
        {
            return "CommandLineArguments";
        }

        const String getApplicationVersion() override
        {
            return "1.0";
        }

    private:

};

START_JUCE_APPLICATION(CommandLineArguments);

FINDINGS

//============================================================================
// Name        : CommandLineArguments
// Author      : Divide To Infinity
// Version     : V1.0
// Copyleft    : Divide To Infinity 2015 - GNU GPL v2
// Description : JUCE Command Line Arguments Demo C++11, Ansi-style
//============================================================================

#include "../JuceLibraryCode/JuceHeader.h"

using namespace std;

class CommandLineArguments : public JUCEApplication
{
    public:
        CommandLineArguments(){}
        ~CommandLineArguments(){}

        void initialise(const String& commandLine) override
        {
            Logger * log = Logger::getCurrentLogger();
            log->writeToLog(getApplicationName());

            // To get the parameters as an array, you can call JUCEApplication::getCommandLineParameters()

            // Now list the command line arguments
            log->writeToLog("See the entered command line parameters below: ");
            log->writeToLog(JUCEApplicationBase::getCommandLineParameters());
            cout << endl; // Empty line : 0

            // Now lets try and do something with the arguments
            // First use the getCommandLineParameterArray which returns a tokenised array
            // of Command Line args separated by whitespace
            StringArray argTokens = StringArray(JUCEApplicationBase::getCommandLineParameterArray());

            for(int i = 0; i < argTokens.size(); ++i)
            {
                // Print out each arg line by line
                log->writeToLog(argTokens[i]);

                // We can use these args to call methods
                if (argTokens[i] == "-i")
                {
                    cmdTest();
                }
                // Then use args with parameters and error messages
            }

        }

        void cmdTest()
        {
            cout << "cmdTest called" << endl;
        }

        void shutdown() override {}

        const String getApplicationName() override
        {
            return "CommandLineArguments";
        }

        const String getApplicationVersion() override
        {
            return "1.0";
        }

        void unhandledException(const std::exception *, const String &sourceFilename, int lineNumber) override
        {
            // Here we can use this method to catch exceptions and generate reports
        }

    private:

};

START_JUCE_APPLICATION(CommandLineArguments);

LINKS
Command Line Arguments discussion on the JUCE forum
JUCEApplicationBase Class docs

Categories
Articles Blog C++11 Coders Codex Design Patterns Development Featured GNU C++ Languages Linux Development Programming Science & Technology Solution Technology UML

Coders Codex::How to Generate C++ Code from a UML Class Diagram in Linux

INTRODUCTION

UML Class Generated in Dia

DISCUSSION

sudo apt-get install dia dia2code

FINDINGS

LINKS
C++ Code Generation from UML on Stack Overflow