Skip to content

Program Arguments

Bryce Plunkett edited this page Dec 20, 2018 · 2 revisions

Receiving and Handling Arguments

When you create a custom program, it extends the ProgramComponent class, which contains an input called programArgs of type any.

This programArgs variable is essentially a JavaScript object where every key describes an argument passed to a program. Currently, there are two ways a program can receive arguments: through a console launched command or a when being used to open a file.

To access these arguments, use this identifier this.programArgs in ngOnInit() or a method called after the ngOnInit hook. If you try accessing this.programsArgs too early (such as in the constructor), the variable will point towards null.

Convenient Argument Parsing

The ProgramComponent class contains a protected method compareToDefaultArguments that lets you pass in two parameter. The first parameter is the args parameter (assumes not null) and the second parameter is the defaultArguments parameter (assumes not null). The method returns a new object with a value for every key of defaultArguments. If the value is in arguments, it uses that value otherwise it will use the default argument.

Currently, this method cannot handle nested objects or arrays.

Method header: protected compareToDefaultArguments (args, defaultArguments): any

Example found in src\app\programs\file-explorer\file-explorer.component.ts:

  private processProgramArguments (args: any): void {
    /**
     * Processes program arguments, such as starting directory
     */
    const defaultArguments = {
      startingDirectory: '/'
    };
    // ensures args isn't null. if so, just use default args
    // fills in gaps with defaults
    let resultArguments;
    if (args != null) {
      resultArguments = super.compareToDefaultArguments(args, defaultArguments);
    } else {
      resultArguments = defaultArguments;
    }
    ...
  }