skip to Main Content

I want to have a Windows executable created with Flutter to be able to accept command arguments and print the result inside my CMD when I execute it like this :

commandtest.exe --type=csv

I wrote this code :

import 'dart:io';

import 'package:args/args.dart';

void main(List<String> args) {
  var parser = ArgParser()..addOption("type", abbr: 't', defaultsTo: 'default');

  try {
    final ArgResults argResults = parser.parse(args);

    final String type = argResults['type'] as String;

    stdout.writeln('Type: $type'); // print() doesn't work either
    exit(0);
  } on Exception catch (e) {
    stderr.writeln("error on parsing args : $e");
    exit(1);
  }
}

Here’s my expected behavior, in my CMD :

path/to/executable> mycommand.exe --type=csv
Type: csv

path/to/executable>

But for now, nothing works and my CMD is empty when i run my command :

path/to/executable> mycommand.exe --type=csv

path/to/executable>

However, I know it works because when I try my command like this :

mycommand.exe --type=csv > output.txt

the output.txt file is created and my result is correctly pushed inside.

I don’t see where’s the problem. Help please ?

2

Answers


  1. The behaviour that you are showing occurs when you run a Windows GUI app from cmd: These do not print anything to the cmd output. They still have a stdout which you can redirect in the way that you showed – but they won’t print it to the console output.

    You probably used flutter create commandtest to create your application. This way, you create a cmake project that builds an app as a windows gui application.

    Instead, use dart create commandtest to create a pure dart app. Copy your code to bincommandtest.dart. Create the exe with dart compile exe bincommandtest.dart.

    Login or Signup to reply.
  2. Summary

    I would recommend making use of the commandline_or_gui_windows package on pub.dev. The package ensures that the console subsystem is used, so your stdout goes to the console. I modified the sample code from the commandline_or_gui_windows example to run your desired code, see the example below. Ensure to run the command after the package is imported, so that the Cpp code is modified before compilation. ref the readme for more details.

    Command

    flutter pub run commandline_or_gui_windows:create

    Code

    // flutter library for gui
    import 'package:flutter/material.dart';
    
    // import of the plugin
    import 'package:commandline_or_gui_windows/commandline_or_gui_windows.dart';
    
    // used to write to stdout and stderr
    import 'dart:io';
    
    // not part of plugin, added to add commandline input
    import 'package:args/args.dart';
    
    /*
      Commandline and dart
        https://dart.dev/tutorials/server/cmdline
      Error codes:
        https://docs.microsoft.com/en-us/windows/win32/debug/system-error-codes--0-499-
     */
    void main(List<String> args) async {
      // create flags
      ArgParser parser = ArgParser()
        ..addOption(
          "type",
          abbr: 't',
          defaultsTo: 'default',
        );
    
      // parse results exit if error
      ArgResults results;
      try {
        // parse
        results = parser.parse(args);
    
      } catch (err) {
        stderr.writeln(err.toString());
        exit(1);
      }
    
      CommandlineOrGuiWindows.runAppCommandlineOrGUI(
        // if there are 1 or more args passed the app will run in commandline mode
        argsCount: args.length,
    
        // if false the app won't close at the end of commandline mode
        // this is allows you to work on code without builing after every change
        // set to true if you want the app to close when commandline finishes
        closeOnCompleteCommandlineOptionOnly: true,
    
        // when in commandline mode run the below function
        commandlineRun: () async {
          try {
            final String type = results['type'] as String;
    
            stdout.writeln('Type: $type'); // print() doesn't work either
            exit(0);
          } catch (e) {
            stderr.writeln("error on parsing args : $e");
            exit(1);
          }
        },
    
        // gui to be shown when running in gui mode
        gui: const MaterialApp(
          home: Scaffold(
            body: Center(
              child: Text("Hello World"),
            ),
          ),
        ),
      );
    }
    
    

    Testing

    debug (-a is for flutter cmd)
    flutter run -a --type -a "A cool type"

    release (sub whatever your app name is commandline_or_gui_windows_example)
    path: buildwindowsx64runnerRelease

    flutter build windows --release
    .commandline_or_gui_windows_example.exe --type "Cool"
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search