« Command Line Parser Library » : différence entre les versions

De Banane Atomic
Aller à la navigationAller à la recherche
 
(14 versions intermédiaires par le même utilisateur non affichées)
Ligne 1 : Ligne 1 :
[[Category:CSharp]]
= Documentation =
= Documentation =
* https://github.com/commandlineparser/commandline
* [https://github.com/commandlineparser/commandline GitHub project]
* [http://commandline.codeplex.com/wikipage?title=The-Option-List-Attribute OptionList]
* [https://github.com/commandlineparser/commandline/wiki/Getting-Started Getting Started]
* [https://commandline.codeplex.com/wikipage?title=The-Value-List-Attribute ValueList]


= Exemple =
= Options =
<kode lang=csharp>
<kode lang=cs>
// créer une classe dont l'instance sera passée à la méthode ParseArguments
// créer une classe dont l'instance sera passée à la méthode ParseArguments
class Options
class Options
{
{
     [HelpOption('h', "help")]
     [Usage(ApplicationAlias = "My Application")]
     public string GetUsage()
     public static IEnumerable<Example> Examples => new List<Example>()
     {
     {
         return
         new Example(
@"-h, --help
            "Description of the application",
affiche l'aide
            new Options { Option1 = "Option1", Option2 = 100 })
    };


-d, --inputdirectory
    [Option('o', "option1", Required = true, HelpText = "Option1.")]
Input directory to use.";
     public string Option1 { get; set; }
     }


    // définition d'une option
     [Option('t', "option2", HelpText = "Option2.")]
     [Option('d', "inputdirectory", Required = false, HelpText = "Input directory to use.")]
     public int Option2 { get; set; }
    public string InputDirectory { get; set; }
 
    [OptionList('o', "options", HelpText = "Liste des options")]
    public IList<string> OptionsList { get; set; }
 
    [Option('v', null, HelpText = "Print details during execution.")]
     public bool Verbose { get; set; }
 
    // récupère tous les arguments qui n'ont été récupérés par une option
    [ValueList(typeof(List<string>))]
    public IList<string> InputFiles { get; set; }
}
}
</kode>
</kode>


<kode lang=csharp>
= [https://github.com/commandlineparser/commandline/wiki/Getting-Started#parsing Parsing] =
var options = new Options();
<kode lang=cs>
if (CommandLine.Parser.Default.ParseArguments(args, options))
static void Main(string[] args)
{
{
     if (options.Verbose) { }
     Parser.Default.ParseArguments<Options>(args)
          .MapResult(
              options => RunAndReturnExitCode(options),
              _ => 1);
</kode>


    var inputDirectory = options.InputDirectory;
== [https://github.com/commandlineparser/commandline/wiki/Getting-Started#using-mapresult-in-asyncawait Parsing with async] ==
      
<kode lang=cs>
    if (options.OptionsList != null && options.OptionsList.Contains("mon_option")) { }
static async Task Main(string[] args)
}
{
     await Parser.Default.ParseArguments<Options>(args)
                .MapResult(
                    async options => await RunAndReturnExitCodeAsync(options),
                    _ => Task.FromResult(1));
</kode>
</kode>


<kode lang=dos>
= Command line =
-h
{{info | In case of missing or unknown argument or parsing error, the errors are displayed with the usage explanation message and the application exits.}}
--inputdirectory="C:\Dossier"
{{info | By default options {{boxx|--help}} and {{boxx|--version}} are available.}}
--inputdirectory "C:\Dossier"
<kode lang=ps>
-i"C:\Dossier"
# option1
--options=mon_options:autre_option
-o value
-omon_options:autre_option
-ovalue
-v
--option1 value
--option1=value
</kode>
</kode>
[[Category:CSharp]]

Dernière version du 26 mars 2020 à 16:39

Documentation

Options

Cs.svg
// créer une classe dont l'instance sera passée à la méthode ParseArguments
class Options
{
    [Usage(ApplicationAlias = "My Application")]
    public static IEnumerable<Example> Examples => new List<Example>()
    {
        new Example(
            "Description of the application",
            new Options { Option1 = "Option1", Option2 = 100 })
    };

    [Option('o', "option1", Required = true, HelpText = "Option1.")]
    public string Option1 { get; set; }

    [Option('t', "option2", HelpText = "Option2.")]
    public int Option2 { get; set; }
}

Parsing

Cs.svg
static void Main(string[] args)
{
    Parser.Default.ParseArguments<Options>(args)
          .MapResult(
              options => RunAndReturnExitCode(options),
              _ => 1);

Parsing with async

Cs.svg
static async Task Main(string[] args)
{
    await Parser.Default.ParseArguments<Options>(args)
                .MapResult(
                    async options => await RunAndReturnExitCodeAsync(options),
                    _ => Task.FromResult(1));

Command line

In case of missing or unknown argument or parsing error, the errors are displayed with the usage explanation message and the application exits.
By default options --help and --version are available.
Ps.svg
# option1
-o value
-ovalue
--option1 value
--option1=value