Code Monkey home page Code Monkey logo

Comments (10)

voieducode avatar voieducode commented on August 23, 2024

Hi currently on holidays with no access to my computer. I will have a look
next week when I am back.

I seem unable to create optional arguments. Many examples on the net show
things such as:

my_app [OPTIONAL]

But when I do this I get a this code generated:

public string ArgOptional { get { return _args["OPTIONAL"].ToString(); } }

So if I don't pass the OPTIONAL argument as soon as I try to access it I
get an exception due to ToString being called on null. As I want this
argument to be optional I expect it to return null or String.Empty not to
throw!?


Reply to this email directly or view it on GitHub
#14.

from docopt.net.

voieducode avatar voieducode commented on August 23, 2024

This is a valid point @jtu100. The default for a string value should be an empty string rather than null.

from docopt.net.

voieducode avatar voieducode commented on August 23, 2024

Just to clarify the override of ToString() in ValueObject should handle this case so I need to check why it does not work.

from docopt.net.

geoff5777 avatar geoff5777 commented on August 23, 2024

Problem I found is that the reference to ValueObject is itself null so modifying ToString() will not help.
I resolved the problem in Options.GenerateCode like this:
return string.Format("public string {0} {{ get {{ return _args[\"{1}\"] == null ? string.Empty : _args[\"{1}\"].ToString(); }} }}", s, Name);

from docopt.net.

jcludwig avatar jcludwig commented on August 23, 2024

My preference would be that the value of an argument never be null, instead have a ValueObject that represents that case. Since that is a breaking change, a reasonable solution might be to add some sort of helper method to check if the value of an arg is null and return a ValueObject with no value if so. Perhaps an extension method for ValueObject?

public static string OptionalValue(this ValueObject self)
{
    return self ?? new ValueObject();
}

Then I can easily write:

string optionalValue = arguments[param].OptionalValue();

from docopt.net.

voieducode avatar voieducode commented on August 23, 2024

@jcludwig An extension method makes sense. @geoff5777 a PR is welcome if you have one.

from docopt.net.

geoff5777 avatar geoff5777 commented on August 23, 2024

The extension method solution is a good solution but I can't see how it helps for those that are using the T4 generated class. At the very least one would still have to modify Options.GenerateCode to use the extension method.

from docopt.net.

csMACnz avatar csMACnz commented on August 23, 2024

I'm also getting stuck on this issue, maybe it would be better to generate helper methods so we can check for optional options without it crashing, and without having to duplicate the flags.

eg. I'm currently using an added method to my T4 file (which is not ideal)

public bool IsProvided(string parameter)
{
    return _args[parameter] != null;
}

but my code ends up with:

var outputFile = args.IsProvided("--output") ? args.OptOutput : string.Empty;

it would be nicer to be able to say:

var outputFile = args.HasOptOutput ? args.OptOutput : string.Empty;

(still would be much nice to just say var outputFile = args.OptOutput ?? string.Empty; though)

Does the IsTrue and AsList generated versions have the same NullReferenceException issue?

from docopt.net.

Comandermuffif avatar Comandermuffif commented on August 23, 2024

Should this issue be closed?

from docopt.net.

voieducode avatar voieducode commented on August 23, 2024

yep

from docopt.net.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.