Skip to content

Commit

Permalink
Merge pull request #6 from SvenGroot/v3.0-dev
Browse files Browse the repository at this point in the history
Merge v3.0-dev into main
  • Loading branch information
SvenGroot authored Dec 1, 2022
2 parents 193686b + 8973dfa commit d7e610a
Show file tree
Hide file tree
Showing 219 changed files with 24,078 additions and 5,622 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ bld/

# Visual Studio 2015/2017 cache/options directory
.vs/
.vscode/
# Uncomment if you have tasks that create the project's static files in wwwroot
#wwwroot/

Expand Down Expand Up @@ -340,4 +341,4 @@ ASALocalRun/
.localhistory/

# BeatPulse healthcheck temp database
healthchecksdb
healthchecksdb
173 changes: 151 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,50 +1,179 @@
# Ookii.CommandLine
# Ookii.CommandLine [![NuGet](https://img.shields.io/nuget/v/Ookii.CommandLine)](https://www.nuget.org/packages/Ookii.CommandLine/)

Ookii.CommandLine enables comprehensive command line argument parsing for .Net applications. It allows you to easily define required, optional, positional and named arguments, parse the command line, and generate usage information. It is provided in versions for .Net Framework 2.0 and later, .Net Standard 2.0, and .Net 6.0 and later.
Ookii.CommandLine is a powerful and flexible command line argument parsing library for .Net
applications.

Ookii.CommandLine can be added to your project in Visual Studio via [NuGet](https://nuget.org/packages/Ookii.CommandLine).
[Code snippets](docs/Code%20Snippets.md) are available on the
- Easily define arguments by creating a class with properties.
- Create applications with multiple subcommands.
- Generate fully customizable usage help.
- Supports PowerShell-like and POSIX-like parsing rules.</Description>

Ookii.CommandLine is provided in versions for [.Net Standard 2.0, .Net Standard 2.1, and .Net 6.0 and later](#requirements).

Ookii.CommandLine can be added to your project using [NuGet](https://nuget.org/packages/Ookii.CommandLine).
[Code snippets](docs/CodeSnippets.md) for Visual Studio are available on the
[Visual Studio marketplace](https://www.ookii.org/Link/CommandLineSnippets).

## Overview

Ookii.CommandLine is a library that helps you to parse command line arguments for your applications. It allows you to easily define a set of accepted arguments, and then parse the command line supplied to your application for those arguments. In addition, it allows you to generate usage help from the arguments that you defined which you can display to the user.
Ookii.CommandLine is a library that lets you parse the command line arguments for your application
into a set of strongly-typed, named values. You can easily define the accepted arguments, and then
parse the command line supplied to your application for those arguments. In addition, you can
generate usage help that can be displayed to the user.

Ookii.CommandLine can be used with any kind of .Net application, whether Console, Windows Forms, or WPF. Although a limited subset of functionality – particularly related around generating usage help text – is geared primarily towards console applications that are invoked from the command prompt, the main command line parsing functionality is usable in any application that needs to process command line arguments.
Ookii.CommandLine can be used with any kind of .Net application, whether console or GUI. Some
functions, such as creating usage help, are primarily designed for console applications, but even
those can be easily adapted for use with other styles of applications.

To define a set of command line arguments, you create a class that will hold their values. The constructor parameters and properties of that class determine the set of arguments that are accepted. Attributes can be used to specify things such as the argument name and whether or not an argument is required, and to specify descriptions used to customize the usage help.
Two styles of [command line parsing rules](docs/Arguments.md) are supported: the default mode uses
rules similar to those used by PowerShell, and the alternative [long/short mode](docs/Arguments.md#longshort-mode)
uses a style influenced by POSIX conventions, where arguments have separate long and short names
with different prefixes. Many aspects of the parsing rules are configurable.

Command line parsing is done in a way that is similar to that used by Windows PowerShell. Each argument has a name, and can be supplied by name on the command line. An argument can also be positional, in which case it can be supplied without the name. Arguments can be required or optional, and there is support for switch arguments (which don't need a value but are either present or not) and arguments with multiple values. Various aspects of the parsing, such as the argument name prefix (typically a `/` or a `-`), can be customized.
To determine which arguments are accepted, you create a class, with constructor parameters and
properties that define the arguments. Attributes are used to specify names, create required or
positional arguments, and to specify descriptions for use in the generated usage help.

For example, the following class defines four arguments: a required positional argument, an optional positional argument, a named argument, and a switch argument:
For example, the following class defines four arguments: a required positional argument, an optional
positional argument, a named-only argument, and a switch argument (sometimes also called a flag):

```csharp
class MyArguments
{
[CommandLineArgument(Position = 0, IsRequired = true)]
public string RequiredArgument { get; set; }
[Description("A required positional argument.")]
public string? Required { get; set; }

[CommandLineArgument(Position = 1)]
public int OptionalArgument { get; set; }
[Description("An optional positional argument.")]
public int Optional { get; set; }

[CommandLineArgument]
public DateTime NamedArgument { get; set; }
[Description("An argument that can only supplied by name.")]
public DateTime Named { get; set; }

[CommandLineArgument]
public bool SwitchArgument { get; set; }
[Description("A switch argument, which doesn't require a value.")]
public bool Switch { get; set; }
}
```

The application using these arguments would have the following usage syntax (this kind of usage help can be generated by Ookii.CommandLine, and can be customized to include argument descriptions):
Each argument has a different type that determines the kinds of values it can accept.

To parse these arguments, all you have to do is add the following line to your `Main` method:

```csharp
var arguments = CommandLineParser.Parse<MyArguments>();
```

This code will take the arguments from `Environment.GetCommandLineArgs()` (you can also manually
pass a `string[]` array if you want), will handle and print errors to the console, and will print
usage help if needed. It returns an instance of `MyArguments` if successful, and `null` if not.

If the arguments are invalid, or help is requested, this application will print the following usage
help:

```text
Usage: MyApplication [-Required] <String> [[-Optional] <Int32>] [-Help] [-Named <DateTime>]
[-Switch] [-Version]
-Required <String>
A required positional argument.
-Optional <Int32>
An optional positional argument.
-Help [<Boolean>] (-?, -h)
Displays this help message.
-Named <DateTime>
An argument that can only supplied by name.
-Switch [<Boolean>]
A switch argument, which doesn't require a value.
-Version [<Boolean>]
Displays version information.
```

The [usage help](docs/UsageHelp.md) includes the descriptions given for the arguments.

See the [documentation for the samples](src/Samples) for more examples of usage help generated by
Ookii.CommandLine. The usage help format can also be [fully customized](src/Samples/CustomUsage).

The application also has two arguments that weren't in the class, `-Help` and `-Version`, which are
automatically added by default.

An example invocation of this application, specifying all the arguments, would look like this
(argument names are case insensitive by default):

```text
./MyApplication foo 42 -switch -named 2022-08-14
```

In addition, Ookii.CommandLine can be used to create applications that have [multiple subcommands](docs/Subcommands.md),
each with their own arguments.

[Try it yourself on .Net Fiddle](https://dotnetfiddle.net/fgLvSl), or
[try out subcommands](https://dotnetfiddle.net/vGIG78).

## Requirements

Ookii.CommandLine is a class library for use in your own applications for [Microsoft .Net](https://dotnet.microsoft.com/).
It can be used with applications supporting one of the following:

- .Net Standard 2.0
- .Net Standard 2.1
- .Net 6.0 and later

As of version 3.0, .Net Framework 2.0 is no longer supported. You can still target .Net Framework
4.6.1 and later using the .Net Standard 2.0 assembly. If you need to support an older version of
.Net, please continue to use [version 2.4](https://github.com/SvenGroot/ookii.commandline/releases/tag/v2.4).

The .Net Standard 2.1 and .Net 6.0 version utilize `ReadOnlySpan<char>` for improved performance of
the [`LineWrappingTextWriter`](docs/Utilities.md) class.

> Usage: MyApplication.exe [-RequiredArgument] \<String> [[-OptionalArgument] \<Number>] [-NamedArgument \<DateTime>] [-SwitchArgument]
The .Net 6.0 version has additional support for [nullable reference types](docs/Arguments.md#arguments-with-non-nullable-types).

An example invocation of this application, specifying all the arguments, would look like this:
## Building and testing

> MyApplication.exe foo 42 -SwitchArgument -NamedArgument 2019-08-14
To build Ookii.CommandLine, make sure you have the .Net 6.0 SDK installed, and simply use the
`dotnet build` command in the `src` directory. You can run the unit tests using `dotnet test`. The
tests should pass on all platforms (Windows and Linux have been tested).

In addition, Ookii.CommandLine can be used to create command line utilities that perform multiple operations, each with their own arguments
(these are called `ShellCommands` in Ookii.CommandLine).
The tests are built and run for both .Net 6.0 and .Net Framework 4.8. Running the .Net Framework
tests on a non-Windows platform may require the use of [Mono](https://www.mono-project.com/).

Ookii.CommandLine uses a strongly-typed resources file, which will not update correctly unless the
`Resources.resx` file is edited with [Microsoft Visual Studio](https://visualstudio.microsoft.com/).
I could not find a way to make this work correctly with both Visual Studio and the `dotnet` command.

The class library documentation is generated using [Sandcastle Help File Builder](https://github.com/EWSoftware/SHFB).

## Comparing with System.CommandLine

Back when Ookii.CommandLine was first written, there was no official way to parse command line
arguments; the only way was a third-party library, or do it yourself.

Nowadays, System.CommandLine offers an official Microsoft solution for command line parsing. Why,
then, should you use Ookii.CommandLine?

Ookii.CommandLine has a very different design. It uses a declarative approach to defining command
line arguments, using properties and attributes, which I personally prefer as it reduces the amount
of code you typically need to write.

Additionally, Ookii.CommandLine supports a more PowerShell-like syntax, as well as the more
POSIX-like syntax that System.CommandLine uses).

In the end, it comes down to personal preference. You should use whichever one suits your needs and
coding style best.

## More information

Please check out the following to get started:
- [Usage documentation](docs/Documentation.md)

- [Tutorial: getting started with Ookii.CommandLine](docs/Tutorial.md)
- [Migrating from Ookii.CommandLine 2.x](docs/Migrating.md)
- [Usage documentation](docs/README.md)
- [Class library documentation](https://www.ookii.org/Link/CommandLineDoc)
- [Sample application](src/CommandLineSampleCS/ProgramArguments.cs) that demonstrates many of the capabilities of Ookii.CommandLine, including explanations
- [ShellCommand sample](src/ShellCommandSampleCS/)
- [Sample applications](src/Samples) with detailed explanations and sample output.
Loading

0 comments on commit d7e610a

Please sign in to comment.