Skip to content
This repository was archived by the owner on Aug 29, 2025. It is now read-only.

Commit 9538de9

Browse files
authored
Add me command substitution (#385)
1 parent c8f77d5 commit 9538de9

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
function Replace-TextInFile {
2+
Param(
3+
[Parameter(Mandatory)]
4+
[string] $FilePath,
5+
[Parameter(Mandatory)]
6+
[string] $Pattern,
7+
[string] $Replacement
8+
)
9+
10+
$input = [System.IO.File]::ReadAllText($FilePath)
11+
$output = $input -creplace $Pattern, $Replacement
12+
if (($input.Length -ne $output.Length) -and ($input -ne $output)) {
13+
[System.IO.File]::WriteAllText(
14+
$FilePath,
15+
$output
16+
)
17+
}
18+
}
19+
20+
function Update-FileContent {
21+
param(
22+
[Parameter(Mandatory)]
23+
[ValidateScript({
24+
Test-Path $_ -PathType Container
25+
}, ErrorMessage="Source dir '{0}' is not a directory")]
26+
[string] $SourceDir,
27+
[Parameter(Mandatory)]
28+
[string] $Pattern,
29+
[string] $Replacement
30+
)
31+
32+
Get-ChildItem -Path $SourceDir -Filter *.cs -Recurse -File | ForEach-Object {
33+
Replace-TextInFile -FilePath $_ -Pattern $Pattern -Replacement $Replacement
34+
}
35+
}
36+
37+
function Update-MeUserIdHelp {
38+
param(
39+
[string] $WorkspaceRootDir = "."
40+
)
41+
$usersDir = Join-Path -Path $WorkspaceRootDir -ChildPath src\generated\Users
42+
Update-FileContent -SourceDir $usersDir -Pattern 'userIdOption = new Option<string>\("--user-id", description: "(.+)"\)' -Replacement 'userIdOption = new Option<string>("--user-id", description: "$1. Use ''me'' for the currently signed in user.")'
43+
}
44+
45+
function Update-UsersMeAlias {
46+
param(
47+
[string] $WorkspaceRootDir = "."
48+
)
49+
$usersRb = Join-Path -Path $WorkspaceRootDir -ChildPath src\generated\GraphClient.cs
50+
Replace-TextInFile -FilePath $usersRb -Pattern 'command = new Command\("users"\);(\r?\n\s*)' -Replacement 'command = new Command("users");$1command.AddAlias("me");$1'
51+
}

src/Program.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,24 @@ static async Task<int> Main(string[] args)
4646
{
4747
Console.InputEncoding = Encoding.UTF8;
4848
Console.OutputEncoding = Encoding.UTF8;
49+
// Replace `me ...` with `users ... --user-id me`
50+
if (args[0] == "me")
51+
{
52+
var hasHelp = Array.Exists(args, static x => x == "--help" || x == "-h" || x == "/?");
53+
var newArgs = hasHelp ? args : new string[args.Length + 2];
54+
newArgs[0] = "users";
55+
for (var i = 1; i < args.Length; i++)
56+
{
57+
newArgs[i] = args[i];
58+
}
59+
if (newArgs.Length > args.Length)
60+
{
61+
newArgs[args.Length] = "--user-id";
62+
newArgs[args.Length + 1] = "me";
63+
args = newArgs;
64+
}
65+
}
66+
4967
var builder = BuildCommandLine()
5068
.UseDefaults()
5169
.UseHost(CreateHostBuilder)

0 commit comments

Comments
 (0)