WebExeBuilder Documentation

getCmdArgs

Category: Application Control

Namespace: web.app.getCmdArgs

Description

Retrieve command line arguments passed to the application when it was launched. This allows your application to accept parameters, file paths, or configuration options from the command line or file associations.

Syntax

const args = await web.app.getCmdArgs();

Parameters

None

Returns

Promise<Array<string>> - Array of command line arguments (excluding the executable path)

Example

Simple example

const args = await web.app.getCmdArgs();
console.log('Arguments:', args);

Practical example

const args = await web.app.getCmdArgs();

if (args.length > 0) {
    const filePath = args[0];
    await openFile(filePath);
}

Advanced example

const args = await web.app.getCmdArgs();

const options = {
    file: null,
    mode: 'view',
    debug: false
};

for (let i = 0; i < args.length; i++) {
    if (args[i] === '--file' && i + 1 < args.length) {
        options.file = args[i + 1];
        i++;
    } else if (args[i] === '--mode' && i + 1 < args.length) {
        options.mode = args[i + 1];
        i++;
    } else if (args[i] === '--debug') {
        options.debug = true;
    }
}

console.log('Options:', options);

if (options.file) {
    await openFile(options.file);
}

Use Cases

  • Open files passed via command line
  • Accept configuration parameters at startup
  • Implement file associations (double-click to open)
  • Parse command line flags and options
  • Launch in different modes (debug, safe mode)
  • Accept URLs or data from other applications
  • Batch processing with command line input
  • Integration with shell scripts or automation

Error Handling

This method does not throw errors. Returns an empty array if no arguments were passed.

const args = await web.app.getCmdArgs();

if (args.length === 0) {
    console.log('No command line arguments provided');
} else {
    console.log('Processing arguments:', args);
}

Performance Tips

  • Call once at startup and cache the result
  • Validate arguments before processing
  • Provide helpful error messages for invalid arguments
  • Document supported command line options
  • web.files.readTextFile() - Read files passed as arguments
  • web.dialogs.openFile() - Alternative file selection method

Best Practices

  • Arguments starting with -- or - are flags/options
  • Index 0 always contains the executable path
  • Use quotes for arguments containing spaces
  • Arguments are case-sensitive on most systems
  • Use = for key-value pairs: --option=value
  • Combine short flags: -abc equals -a -b -c

Notes

  • Arguments are passed when launching the executable
  • Example: MyApp.exe --file "C:\document.txt" --mode edit
  • Returns array like: ['--file', 'C:\\document.txt', '--mode', 'edit']
  • Executable path is not included in the array
  • Empty array if no arguments provided
  • Useful for file associations and automation