Skip to content

Commit fb52992

Browse files
committed
Implement command-line
1 parent 43af1a2 commit fb52992

4 files changed

Lines changed: 75 additions & 11 deletions

File tree

Src/CommandLine.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
using System.IO;
3+
using System.Linq;
4+
using RT.Util;
5+
using RT.Util.CommandLine;
6+
using RT.Util.Consoles;
7+
using RT.Util.ExtensionMethods;
8+
9+
namespace EsotericIDE
10+
{
11+
[CommandLine, DocumentationLiteral("Opens the Interpreter//Debugger Engine for esoteric programming languages (Esoteric IDE).")]
12+
sealed class CommandLine : ICommandLineValidatable
13+
{
14+
[IsPositional, DocumentationLiteral("Specifies a path and filename of a file to open.")]
15+
public string Filename = null;
16+
17+
[Option("-l", "--language"), DocumentationLiteral("Specifies a programming language to pre-select in the language drop-down.")]
18+
public string Language = null;
19+
20+
[Ignore]
21+
public ProgrammingLanguage LanguagePreselect = null;
22+
23+
private static void PostBuildCheck(IPostBuildReporter rep)
24+
{
25+
CommandLineParser.PostBuildStep<CommandLine>(rep, null);
26+
}
27+
28+
public ConsoleColoredString Validate()
29+
{
30+
if (Filename != null && !File.Exists(Filename))
31+
return "The specified file does not exist.";
32+
33+
if (Language != null)
34+
{
35+
LanguagePreselect = Mainform.Languages.FirstOrDefault(pl => pl.LanguageName.Equals(Language, StringComparison.InvariantCultureIgnoreCase));
36+
if (LanguagePreselect == null)
37+
return "The specified programming language does not exist. Currently support programming languages are: " + Mainform.Languages.Select(pl => pl.LanguageName).JoinString(", ");
38+
}
39+
40+
return null;
41+
}
42+
}
43+
}

Src/EsotericIDE.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
<Compile Include="Brainfuck\Settings.cs" />
6363
<Compile Include="Brainfuck\Enums.cs" />
6464
<Compile Include="ClassifySubstitutions.cs" />
65+
<Compile Include="CommandLine.cs" />
6566
<Compile Include="FontSpec.cs" />
6667
<Compile Include="Hexagony\InputMode.cs" />
6768
<Compile Include="Hexagony\PointAxial.cs" />

Src/EsotericIDEProgram.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Drawing;
32
using System.Reflection;
43
using System.Runtime.InteropServices;
54
using System.Text;
@@ -41,7 +40,7 @@ static int Main(string[] args)
4140

4241
Application.EnableVisualStyles();
4342
Application.SetCompatibleTextRenderingDefault(false);
44-
Application.Run(new Mainform(Settings));
43+
Application.Run(new Mainform(Settings, args));
4544

4645
Settings.Save(onFailure: SettingsOnFailure.ShowRetryWithCancel);
4746
return 0;

Src/Mainform.cs

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.Drawing;
43
using System.IO;
54
using System.Linq;
65
using System.Reflection;
76
using System.Windows.Forms;
8-
using RT.Util;
7+
using RT.Util.CommandLine;
98
using RT.Util.Controls;
109
using RT.Util.Dialogs;
1110
using RT.Util.ExtensionMethods;
@@ -17,12 +16,34 @@ partial class Mainform : ManagedForm, IIde
1716
{
1817
private bool _splitterDistanceBugWorkaround;
1918

20-
public Mainform(EsotericIDE.Settings settings)
19+
public Mainform(EsotericIDE.Settings settings, string[] cmdArgs)
2120
: base(settings.FormSettings)
2221
{
2322
InitializeComponent();
2423
Icon = Resources.EsotericIDEIcon;
2524
init();
25+
26+
try
27+
{
28+
var cmd = CommandLineParser.Parse<CommandLine>(cmdArgs);
29+
30+
if (cmd.Filename != null)
31+
openCore(cmd.Filename);
32+
33+
if (cmd.Filename != null && cmd.LanguagePreselect == null)
34+
{
35+
// Try to guess language from file extension
36+
var ext = Path.GetExtension(cmd.Filename).Substring(1);
37+
cmd.LanguagePreselect = Languages.FirstOrDefault(pl => pl.DefaultFileExtension.Equals(ext, StringComparison.InvariantCultureIgnoreCase));
38+
}
39+
40+
if (cmd.LanguagePreselect != null)
41+
cmbLanguage.SelectedItem = cmd.LanguagePreselect;
42+
}
43+
catch (CommandLineParseException px)
44+
{
45+
txtSource.Text = px.GetUsageInfo().ToString();
46+
}
2647
}
2748

2849
private void init()
@@ -48,16 +69,16 @@ private void init()
4869
ctSplit.SplitterDistance = (int) (ctSplit.Height * EsotericIDEProgram.Settings.SplitterPercent);
4970
};
5071

51-
_languages = Assembly.GetExecutingAssembly().GetTypes()
72+
Languages = Assembly.GetExecutingAssembly().GetTypes()
5273
.Where(t => typeof(ProgrammingLanguage).IsAssignableFrom(t) && !t.IsAbstract)
5374
.Select(t => (ProgrammingLanguage) Activator.CreateInstance(t))
5475
.OrderBy(t => t.LanguageName)
5576
.ToArray();
5677
LanguageSettings settings;
57-
foreach (var lang in _languages)
78+
foreach (var lang in Languages)
5879
if (EsotericIDEProgram.Settings.LanguageSettings.TryGetValue(lang.LanguageName, out settings) && settings != null)
5980
lang.Settings = settings;
60-
cmbLanguage.Items.AddRange(_languages);
81+
cmbLanguage.Items.AddRange(Languages);
6182

6283
ToolStripMenuItem[] currentLanguageSpecificMenus = null;
6384
cmbLanguage.SelectedIndexChanged += (_, __) =>
@@ -72,11 +93,11 @@ private void init()
7293
currentLanguageSpecificMenus = _currentLanguage.CreateMenus(this);
7394
ctMenu.Items.AddRange(currentLanguageSpecificMenus);
7495
};
75-
var ll = _languages.IndexOf(lang => lang.LanguageName == EsotericIDEProgram.Settings.LastLanguageName);
96+
var ll = Languages.IndexOf(lang => lang.LanguageName == EsotericIDEProgram.Settings.LastLanguageName);
7697
cmbLanguage.SelectedIndex = ll == -1 ? 0 : ll;
7798
}
7899

79-
private ProgrammingLanguage[] _languages;
100+
public static ProgrammingLanguage[] Languages;
80101
private ProgrammingLanguage _currentLanguage;
81102

82103
private string _input
@@ -255,7 +276,7 @@ private void open(object _, EventArgs __)
255276
if (!canDestroy())
256277
return;
257278

258-
var filter = _languages
279+
var filter = Languages
259280
.OrderByDescending(lang => lang == _currentLanguage)
260281
.ThenBy(lang => lang.LanguageName)
261282
.Select(lang => "{1} (*.{0})|*.{0}".Fmt(lang.DefaultFileExtension, lang.LanguageName))

0 commit comments

Comments
 (0)