gsscoder / commandline

Terse syntax C# command line parser for .NET with F# support
1.63k stars 293 forks source link

Readme.md missing a VB.NET example #333

Open jochenwezel opened 8 years ago

jochenwezel commented 8 years ago

you might want to provide a VB.NET sample at readme.md or other appropriate documentation

Option Explicit On
Option Strict On

Imports CommandLine

Module MainApp

    Sub Main(args As String())
        Dim result As CommandLine.ParserResult(Of Options)
        result = CommandLine.Parser.Default.ParseArguments(Of Options)(args)
        Dim exitCode As Integer = result.MapResult(AddressOf Start, AddressOf ParserErrors)
        System.Environment.Exit(exitCode)
    End Sub

    Private Function Start(options As Options) As Integer
        Try
            'Do some stuff here...
            Return Nothing '-> exit code = 0
        Catch ex As Exception
            Return 2
        End Try
    End Function

    Private Function ParserErrors(errors As IEnumerable(Of [Error])) As Integer
        Console.WriteLine("Please resolve errors above, first")
        Return 1
    End Function

End Module
dirkrombauts commented 8 years ago

Cool ... the fastest way to get something included in an open source project on GitHub is to include it yourself and send a pull request :-)

jochenwezel commented 8 years ago

I'm sure, you are fast! ;-)

nemec commented 8 years ago

@jochenwezel that's a great idea. However, it's a bit hampered by the fact that I don't know VB.NET :-)

I can add the above example to the readme, but would you be willing to provide equivalent examples to those in the notes section of the readme? We have options and verbs examples for C# and F# but nothing for VB.NET.

jochenwezel commented 8 years ago

I tried to use these samples as my starting point - and failed Maybe I failed due to my non-professional C# knowledge - maybe because there are already mistakes in the C# code sample of the notes page

static int Main(string[] args) {
  var result = CommandLine.Parser.Default.ParseArguments<Options>(args);
  var exitCode = result
    .MapResult(
      options = > {
        if (options.Verbose) Console.WriteLine("Filenames: {0}", string.Join(",", options.InputFiles.ToArray()));
        return 0; },
      errors => {
        LogHelper.Log(errors);
        return 1; });
  return exitCode;

This is the options declaration sample in VB.Net (I added an additional line for a sample of the AssemblyUsage attribute, usually located in the very same class file; but I'm not sure if I understood the declarations below correctly to provide a correct usage description -> please double-check!)

<Assembly: CommandLine.Text.AssemblyUsage("MyApp.exe -r {inputFiles} [-v {true|false}] [--language {name}] [--offset {index}]")>
Class Options
    <CommandLine.Option('r', "read", Required := true,
    HelpText:="Input files to be processed.")>
    Public Property InputFiles As IEnumerable(Of String)

    ' Omitting long name, default --verbose
    <CommandLine.Option(
    HelpText:="Prints all messages to standard output.")>
    Public Property Verbose As Boolean

    <CommandLine.Option(Default:="中文",
    HelpText:="Content language.")>
    Public Property Language As String

    <CommandLine.Value(0, MetaName:="offset",
    HelpText:="File offset.")>
    Public Property Offset As Long?
End Class
nemec commented 8 years ago

I started with defining example classes in 9ff3c90990218392358c21e536cb0f918f63edb1, but I have yet to add equivalent Main method examples for VB.NET.

Pitterling commented 8 years ago

It would be great to have the Demo folder extended with Demos for C# and VB.Net Would that require a restructuring of the Folder?

nemec commented 7 years ago

You can't put C# and VB.Net code in the same project, but if you want to add another project to the demo folder called ReadText.Demo.VB that would be fine.