Comments (4)
Sounds interesting, looking forward to see how it works out! You might be the first one to implement code generation for docopt.
I don't know this T4 stuff :-), but I would guess that usually you need a single usage.txt
per project, no?
from docopt.net.
T4 is a design time code generator integrated in Visual Studio. It's a little bit ugly but it's really powerful.
You're right: in 90% of the use cases there will be a single usage.txt. However looking at the git example, it sounds like there could be multiple usage.txt if the implementor chooses to group all the command line parsing in one project.
from docopt.net.
So I managed to put together a working version of the code generator. It was much easier than I expected. Sample generated code here.
Design
I have introduced a Docopt.GenerateCode()
method that parses out the doc string and scans through the resulting flattened list of LeafPattern
objects to generate code. Basically the code generation is hardcoded in the GenerateCode()
method implementation in the LeafPattern
subclasses. Argument
instances produce "ArgXXX" fields. Command
instances produce "CmdXXX" fields. Option
instances produce "OptXXX" fields.
The original arguments dictionary is still available through an Args
property. In fact the generated code is just a wrapper around this dictionary.
In a second iteration, I will move the code generation code out of the core docopt implementation as I don't feel it really belong there. It should be in the T4 module.
Worked example
Main.usage.txt
Test. Usage: prog command ARG FILES... [-o --switch --long=ARG]
Generated code: T4DocoptNet.cs
// Generated class for Main.usage.txt
public class MainArgs
{
public const string USAGE = @"Test.
Usage: prog command ARG FILES... [-o --switch --long=ARG]
";
private readonly IDictionary<string, ValueObject> _args;
public MainArgs(ICollection<string> argv, bool help = true,
object version = null, bool optionsFirst = false, bool exit = false)
{
_args = new Docopt().Apply(USAGE, argv, help, version, optionsFirst, exit);
}
public IDictionary<string, ValueObject> Args
{
get { return _args; }
}
public bool CmdCommand { get { return _args["command"].IsTrue; } }
public string ArgArg { get { return _args["ARG"].ToString(); } }
public bool OptO { get { return _args["-o"].IsTrue; } }
public string OptLong { get { return _args["--long"].ToString(); } }
public bool OptSwitch { get { return _args["--switch"].IsTrue; } }
public ArrayList ArgFiles { get { return _args["FILES"].AsList; } }
}
Using the generated code
class Program
{
static void DoStuff(string arg, bool flagO, string longValue)
{
// ...
}
static void Main(string[] args)
{
var args = new MainArgs(args);
if (args.CmdCommand)
{
Console.WriteLine("First command");
DoStuff(args.ArgArg, args.OptO, args.OptLong);
}
}
}
Planned improvements
- Add unit tests
- Move the code generation logic out of the Docopt module.
- Enable using embedded resources to avoid repeating the usage string in the generated class.
- Allow custom specification of int options i.e. -vvv
- Add the T4 files to the nuget package.
from docopt.net.
👍
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
- published 0.6.1.9 includes 0.6.1.4 dll HOT 3
- 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.