dotnet / dotnet/command-line-api

How to use System.CommandLine in WinForm?

Open
#2,452 6 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
3.7k
Forks
428
PR merge metrics
No merged PRs in 30d

Description

I don't need output to console, but I need to quickly import data via parameters. Is it possible to parse with `System.CommandLine` inside WinForm? I set up the following code, but nothing is prompted after running.
```C#
using System.CommandLine;

namespace Move_Sample_Folder
{
internal static class Program
{
///
/// The main entry point for the application.
///
[STAThread]
static void Main(string[] args)
{
var foldersArgument = new Argument
(name: "folders",
description: "Folders that need to be moved.");

var destinationOption = new Option(
name: "--destination",
description: "The file to read and display on the console.");
destinationOption.AddAlias("-dst");

var autoOption = new Option(
name: "--auto",
description: "Auto start process.");
autoOption.AddAlias("-a");

var rootCommand = new RootCommand("Move/Rename SkyScan Dataset Folder acquire by Sample Changer to new Destination.");
rootCommand.AddOption(destinationOption);
rootCommand.AddOption(autoOption);
rootCommand.Add(foldersArgument);

rootCommand.SetHandler((foldersValue, destinationOptionValue, autoOptionValue) =>
{
MessageBox.Show($"folders = {foldersValue}");
MessageBox.Show($"--destination = {destinationOptionValue}");
MessageBox.Show($"--auto = {autoOptionValue}");
},
foldersArgument, destinationOption, autoOption);

rootCommand.Invoke(args);

// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize();
Application.Run(new MainForm(args));
}
}
}
```

![devenv_SkyScan_(正在运行)_-_Microsoft_Visual_Studio_0204](https://github.com/dotnet/command-line-api/assets/6565860/6cbdde97-fe8b-4638-984d-f5c1e77b718e)

---

Also, as for #1389, I understand it to be in this form.
For example, Notepad2 and DataViewer are desktop programs, but support many startup parameters. While `-?`, an additional window will pop up to display the help content

![TotalCMD64_Total_Commander_(x64)_11 03_-_David_Attie_0206](https://github.com/dotnet/command-line-api/assets/6565860/230a07ae-d3ef-48a8-8fab-94e984afa48d)

![DataViewer_Untitled_-_DataViewer_0205](https://github.com/dotnet/command-line-api/assets/6565860/2ae4f395-8d51-46dc-964d-238f5b3fb883)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.