Comments (10)
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.
This is a valid point @jtu100. The default for a string value should be an empty string rather than null.
from docopt.net.
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.
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.
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.
@jcludwig An extension method makes sense. @geoff5777 a PR is welcome if you have one.
from docopt.net.
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.
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.
Should this issue be closed?
from docopt.net.
yep
from docopt.net.
Related Issues (20)
- Sync PR builds not occurring during CI on AppVeyor
- Argument that is both boolean and string (git exec-path option) HOT 1
- Assembly version is 0.0.0.0 HOT 1
- Source generator fails when arguments classes are named the same but belong to different namespaces
- Source generator fails with arguments classes in separate files
- Nested class using `[DocoptArguments]` fails to build
- Make entire code base null-safe
- Leading dot in generated hint name when project has no namespace
- Bad example or bug in new parser implementation? HOT 2
- Add MatchAsync to `IParser<T>.IResult` HOT 2
- Reinstalling docopt.net
- Code style errors can break building of user project
- Feature Request: Description in summary comments HOT 2
- Docopt not parsing when running with 'dotnet run' HOT 3
- IsNullOrEmpty on a command throws System.NullReferenceException
- spaces in filenames HOT 3
- Upgrade to netstandard2.0 HOT 1
- run naval_fate HOT 2
- condition to properly build the project and have it work correctly HOT 8
- T4 template doesn't run in "T4DocoptNetHostApp" HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from docopt.net.