Click here to Skip to main content
15,886,963 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: Supporting multiple cores from bare metal C Pin
honey the codewitch15-Feb-24 0:03
mvahoney the codewitch15-Feb-24 0:03 
GeneralRe: Supporting multiple cores from bare metal C Pin
11917640 Member 15-Feb-24 1:04
11917640 Member 15-Feb-24 1:04 
GeneralRe: Supporting multiple cores from bare metal C Pin
honey the codewitch15-Feb-24 5:03
mvahoney the codewitch15-Feb-24 5:03 
GeneralSource Generators in C# and TDD misadventure time Pin
honey the codewitch31-Jan-24 6:30
mvahoney the codewitch31-Jan-24 6:30 
GeneralRe: Source Generators in C# and TDD misadventure time Pin
Graeme_Grant31-Jan-24 21:18
mvaGraeme_Grant31-Jan-24 21:18 
GeneralRe: Source Generators in C# and TDD misadventure time Pin
honey the codewitch1-Feb-24 5:58
mvahoney the codewitch1-Feb-24 5:58 
GeneralRe: Source Generators in C# and TDD misadventure time Pin
Graeme_Grant1-Feb-24 8:31
mvaGraeme_Grant1-Feb-24 8:31 
GeneralCommand line fun in .NET Pin
honey the codewitch27-Jan-24 7:10
mvahoney the codewitch27-Jan-24 7:10 
void Main(string[] args) is broken.

The issue is this: It dequotes values but doesn't tell you which ones were quoted. So if you naively use these as arguments, you will interpret the literal string "/ifstale" as a command line switch /ifstale instead of a literal string. Yes, the problem is a bit contrived, but it's kind of scary to think about the possibilities where something like it can crop up. It flies in the face of application robustness.

My solution involves Environment.CommandLine which reports the raw command line argument, quotes and all.

The issue with that is sometimes your .NET assembly is hosted by another executable, like dotnet.exe and then your arguments are all off by two.

Here's what i have to do. Basically what I'm doing is making the switch -- normally, but / for windows, primarily because of file path issues causing ambiguities with /

After that I crack the command line arguments, getting the exe name separately. I didn't post that code but it's here if you're interested: Environment.CommandLine cracker - Pastebin.com[^]

C#
var prefix = "--";
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
    prefix = "/";
}
var cl = Environment.CommandLine;
string exename;
var clargs = CrackCommandLine(cl, out exename);
if (clargs.Count >= args.Length)
{
    clargs = clargs.GetRange(clargs.Count - args.Length, args.Length);
}
else
{
    clargs.Clear();
    for (int i = 0; i < args.Length; i++)
    {
        clargs.Add(new KeyValuePair<bool, string>(false, args[i]));
    }
}



Then - get this - I have to count the arguments backward from the end to match them up to your actual executable's arguments, and that's the only thing the args[] array is good for.

Now at the end of that code, you have keyvaluepair arguments where the Key is whether it's quoted or not, and the Value is the de-quoted, de-escaped value in any case. It's dirty, but it works.
Check out my IoT graphics library here:
https://honeythecodewitch.com/gfx
And my IoT UI/User Experience library here:
https://honeythecodewitch.com/uix

GeneralRe: Command line fun in .NET Pin
PIEBALDconsult27-Jan-24 7:24
mvePIEBALDconsult27-Jan-24 7:24 
GeneralRe: Command line fun in .NET Pin
honey the codewitch27-Jan-24 7:37
mvahoney the codewitch27-Jan-24 7:37 
GeneralMicrosoft example includes obsolete code: Why? Pin
raddevus25-Jan-24 2:15
mvaraddevus25-Jan-24 2:15 
GeneralRe: Microsoft example includes obsolete code: Why? Pin
Bruno van Dooren25-Jan-24 5:28
mvaBruno van Dooren25-Jan-24 5:28 
GeneralRe: Microsoft example includes obsolete code: Why? Pin
raddevus25-Jan-24 8:50
mvaraddevus25-Jan-24 8:50 
GeneralFound an interesting algorithm Pin
honey the codewitch21-Jan-24 5:16
mvahoney the codewitch21-Jan-24 5:16 
GeneralRe: Found an interesting algorithm Pin
Nelek21-Jan-24 6:15
protectorNelek21-Jan-24 6:15 
GeneralRe: Found an interesting algorithm Pin
Greg Utas21-Jan-24 13:32
professionalGreg Utas21-Jan-24 13:32 
GeneralMicrosoft Regex Weirdness Pin
honey the codewitch7-Jan-24 4:17
mvahoney the codewitch7-Jan-24 4:17 
GeneralRe: Microsoft Regex Weirdness Pin
PIEBALDconsult7-Jan-24 5:30
mvePIEBALDconsult7-Jan-24 5:30 
GeneralRe: Microsoft Regex Weirdness Pin
k50547-Jan-24 7:34
mvek50547-Jan-24 7:34 
GeneralRe: Microsoft Regex Weirdness Pin
PIEBALDconsult7-Jan-24 7:59
mvePIEBALDconsult7-Jan-24 7:59 
GeneralRe: Microsoft Regex Weirdness Pin
Brisingr Aerowing7-Jan-24 8:40
professionalBrisingr Aerowing7-Jan-24 8:40 
GeneralRe: Microsoft Regex Weirdness Pin
PIEBALDconsult7-Jan-24 8:58
mvePIEBALDconsult7-Jan-24 8:58 
GeneralRe: Microsoft Regex Weirdness Pin
honey the codewitch7-Jan-24 14:40
mvahoney the codewitch7-Jan-24 14:40 
GeneralRe: Microsoft Regex Weirdness Pin
PIEBALDconsult7-Jan-24 15:34
mvePIEBALDconsult7-Jan-24 15:34 
GeneralRe: Microsoft Regex Weirdness Pin
honey the codewitch7-Jan-24 15:49
mvahoney the codewitch7-Jan-24 15:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.