AX for Retail: Custom command-line arguments for POS training mode

We recently had a scenario where a customer wanted to be able to start the POS in training mode directly from an shortcut instead of having the user go in and select training mode from the menu.  They would then have two icons for POS:  one for “live” operation and one for training.  Usually this would entail using a command-line parameter to represent whether training mode would be set or not and then creating the shortcut with that parameter.

Unfortunately the POS does not have such a command-line argument.  But that doesn’t mean you can’t add one yourself.

Normally command-line arguments are read during application start up in the Main() method.  But .Net has a not-so-widely-used method in the Environment class that can be used to re-read them at any point:

Environment.GetCommandLineArgs()

This returns an array of strings just like the Main() method gets.  You can loop through this and see if your string is included:

 

  1: foreach (string arg in Environment.GetCommandLineArgs())
  2: {
  3:     if (arg.StartsWith("-ATRAIN", StringComparison.OrdinalIgnoreCase))
  4:     {
  5: 		//do stuff
  6:     }
  7: }
  8:

 

Note that this is a very simplistic way of checking command line arguments – there are plenty of parsing libraries out there if you want to add more flexibility.  Also note that I used “-ATRAIN” instead of “-TRAIN” because the POS app already has a monopoly on arguments that start with “-TR” (see the article AX for Retail: Modifying POS Labels and Strings for more about the usefulness of that flag).

So now that we know we can read our custom command-line argument, the question is finding a place to put it.  You have the option of simply calling GetCommandLineArgs() any time you need to check for it (since persisting it to an object may be difficult) or if you truly only need it at startup, you can put it in the ApplicationStart() method of the Application triggers.  In our example, we just want to start the POS in training mode which only needs to happen once during startup:

 

  1:         public void ApplicationStart()
  2:         {
  3:             LSRetailPosis.ApplicationLog.Log("IApplicationTriggers.ApplicationStart", "Application has started", LSRetailPosis.LogTraceLevel.Debug);
  4: 
  5:             foreach (string arg in Environment.GetCommandLineArgs())
  6:             {
  7:                 if (arg.StartsWith("-ATRAIN", StringComparison.OrdinalIgnoreCase))
  8:                 {
  9:                     LSRetailPosis.ApplicationLog.Log("IApplicationTriggers.ApplicationStart", "Training Mode == true", LSRetailPosis.LogTraceLevel.Debug);
 10:                     LSRetailPosis.Settings.ApplicationSettings.Terminal.TrainingMode = true;
 11:                 }
 12:             }
 13:         }

 

 

Deploying this custom ApplicationTriggers.dll to your POS folder will do two things:  write a quick message to the RetailLog table and flip the “TrainingMode” flag to true.  This will force the application to training mode before the logon screen even appears.

To utilize this this, option, create a desktop shortcut and add the argument after POS.exe:

 

ScreenShot1

 

Another advantage of this customization:  you can turn on “Hide Training Mode” so that the only way to get into training mode is by launching the shortcut.  This will allow you to prevent users from switching between training mode and live mode on the logon screen:

ScreenShot2 screenshot3  Screenshot4

You can use custom command-line arguments for other purposes as well – it is a very simple way to “store” settings (as opposed to the POS.exe.config file, the registry, etc.).  If you think of any other creative uses, feel free to toss a note in the comments section below.

[Note:  this post was written with AX for Retail 2012 in mind.  The command-line argument trick should work in 2009 but I’m not sure if turning on training mode is as simple.]