Home
Categories
Dictionary
Download
Project Details
Changes Log
Tutorials
FAQ
License

Command-line



The Command-line handler framework is an elaborate framework allowing to take into account the command-line arguments which are managed by the application and each Plugin.

CommandLine listener

The CommandLineListener class is an interface (implemented by the application and each of the Plugins) which handles the command-line arguments:
To handle command-line arguments in your listener, you must implement both these methods. By default the getCommandLineConfiguration() method returns null, which means that the listener will not handle any command-line arguments.

Application listener

The AbstractApplication class is a CommandLine listener, but it has several additional methods to manage the arguments:
To handle command-line arguments in your application and its Plugins, you must implement both these methods. The default implementations do nothing, which means that by default the command-line arguments won't be handled by both the application and the plugins.

Arguments file


The XML file specifying the arguments managed by the listener is returned by the CommandLineListener.getCommandLineConfiguration() method.

For example:
      public class MyMDI extends AbstractMDIApplication {

        public MyMDI(String[] args) {
          super("My MDI");
          ...
        }

        @Override
        public URL getCommandLineConfiguration() {
          return this.getClass().getResource("commandline.xml");
        }

      }

Example

First example: default handling of command-line arguments

The following example shows how an application can handle arguments. In this example, we have the following commandline.xmlarguments file:
      <arguments>
         <argument key="sizeX" type="int" />
         <argument key="sizeY" type="int" />
      </arguments>

As you see, we declare that we handle two sizeX and sizeY int arguments which specify the size of our window.


And the following code for the application:
      public class MyMDI extends AbstractMDIApplication {
        private int sizeX = 200;
        private int sizeY = 200;

        public MyMDI(String[] args) {
          super("My MDI");
          super.preparePanels();

          // set the command-line arguments
          this.setCommandLineArguments(args);
          // apply the arguments for the application (it will indirectly call the handleCommandLineArguments method)
          super.applyCommandLineArguments();
          // set the size of the Application window
          this.setSize(sizeX, sizeY);
        }

        @Override
        public URL getCommandLineConfiguration() {
          // specify the xml file specifying the arguments
          return this.getClass().getResource("commandline.xml");
        }

        @Override
        public void handleCommandLineArguments(Map<String, ArgumentGroup> argumentGroups, Map<String, Argument> arguments) {
          // handle the arguments
          if (arguments.containsKey("sizeX")) {
            Argument arg = arguments.get("sizeX");
            sizeX = (Integer)arg.getValue();
          }
          if (arguments.containsKey("sizeY")) {
            Argument arg = arguments.get("sizeY");
            sizeY = (Integer)arg.getValue();
          }
      }

        public static void main(String[] args) {
          MyMDI mdi = new MyMDI(args);
          mdi.setVisible(true);
        }
      }
We can for example start the application with:
      java -jar MyMDI.jar -sizeX=800 -sizeY=300
or even with:
      java -jar MyMDI.jar sizeX=800 sizeY 300

First example: keeping dash characters in command-line arguments

In this example, will explicitely specify our arguments with dash characters in the commandline.xmlarguments file:
      <arguments>
         <argument key="-sizeX" type="int" />
         <argument key="-sizeY" type="int" />
      </arguments>
The code for the application will be almost identical to the previous example:
      public class MyMDI extends AbstractMDIApplication {
        private int sizeX = 200;
        private int sizeY = 200;

        public MyMDI(String[] args) {
          super("My MDI");
          super.preparePanels();

          // set the command-line arguments
          this.setCommandLineArguments(args, true);
          // apply the arguments for the application (it will indirectly call the handleCommandLineArguments method)
          super.applyCommandLineArguments();
          // set the size of the Application window
          this.setSize(sizeX, sizeY);
        }

        @Override
        public URL getCommandLineConfiguration() {
          // specify the xml file specifying the arguments
          return this.getClass().getResource("commandline.xml");
        }

        @Override
        public void handleCommandLineArguments(Map<String, ArgumentGroup> argumentGroups, Map<String, Argument> arguments) {
          // handle the arguments
      
             if (arguments.containsKey("-sizeX")) {
               Argument arg = arguments.get("-sizeX");
               sizeX = (Integer)arg.getValue();
             }
             if (arguments.containsKey("-sizeY")) {
               Argument arg = arguments.get("-sizeY");
               sizeY = (Integer)arg.getValue();
             }
      
      }

        public static void main(String[] args) {
          MyMDI mdi = new MyMDI(args);
          mdi.setVisible(true);
        }
      }
We can for example start the application with:
      java -jar MyMDI.jar -sizeX=800 -sizeY=300

Notes

  1. ^ See LauncherConf for more information

See also


Categories: commandline

Copyright 2006-2023 Herve Girod. All Rights Reserved. Documentation and source under the LGPL v2 and Apache 2.0 licences