question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Designate Required vs Optional Parameters/Options

See original GitHub issue

Currently, it is up to the application developer using CommandLineUtils to validate command line input and display an appropriate error if an Option that is required is left out. I propose that CommandLineUtils take on this responsibility, allowing the library user to designate parameters as required and provide appropriate feedback to the user when the parameter is not provided.

This may be a nice feature to do after #8 so the user of the library need only add an attribute of [Required] to make this designation.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
natemcmastercommented, Dec 2, 2017

Thought about it more, here is what I’m thinking. Would like your feedback.

Attributes

Design:

Support any implementation of System.ComponentModel.DataAnnotations.ValidationAttribute. This includes RequiredAttribute and others.

Result: When validation fails, the application exits with code 1 and prints an error message.

Usage:

using System.ComponentModel.DataAnnotations;
using McMaster.Extensions.CommandLineUtils;

class Program
{
    static int Main(string[] args) => CommandLineApplication.Execute<Program>(args);

    [Required]
    [Option(Description = "Required. The message")]
    private string Message { get; }

    [Required(ErrorMessage = "my custom error message")]
    [Option("--to <EMAIL>", Description = "Required. The recipient.")]
    public string To { get; }

    private void OnExecute(IConsole console)
    {
        console.WriteLine("To = " + To);
        console.WriteLine("Message = " + Message);
    }
}

Builder api

Design: Add extension methods that build of CommandOption, such as .IsRequired(). These should roughly map to the same values provided by RequiredAttribute and others.

Usage:

class Program
{
    static int Main(string[] args)
    {
        var app = new CommandLineApplication();

        var optionMessage = app.Option("-m|--message <MSG>", "Required. The message.", CommandOptionType.SingleValue)
            .IsRequired();

        var optionReceiver = app.Option("--to <EMAIL>", "Required. The recipient.", CommandOptionType.SingleValue)
            .IsRequired();
        app.OnExecute(() =>
        {
            Console.WriteLine("To = " + optionReceiver.Value());
            Console.WriteLine("Message = " + optionMessage.Value());
        });

        return app.Execute(args);
    }
}
0reactions
natemcmastercommented, Dec 7, 2017
Read more comments on GitHub >

github_iconTop Results From Across the Web

in python, how do you denote required parameters and ...
Parameters can be required or optional depending on how they appear in the function ... You can denote required keyword argument as follows:...
Read more >
Named and Optional Arguments - C# Programming Guide
Optional arguments. The definition of a method, constructor, indexer, or delegate can specify its parameters are required or optional.
Read more >
Javascript: Declaring Optional Function Paramters
By definition, an Optional Parameter is a handy feature that enables programmers to pass less number of parameters to a function and assign...
Read more >
Using Python Optional Arguments When Defining Functions
In this tutorial, you'll learn about Python optional arguments and how to define functions with default values. You'll also learn how to create...
Read more >
TypeScript Optional Parameters
In this tutorial, you will learn how to use the TypeScript optional parameters for functions.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found