-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Closed
Labels
Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifResolution-No ActivityIssue has had no activity for 6 months or moreIssue has had no activity for 6 months or moreWG-Languageparser, language semanticsparser, language semantics
Description
Wouldn't it be cool if this or something similar would be possible...
# https://regex101.com/r/EpiXHF/2
[regex]$regEx = @"
(?mx-i)
^
(?<amount>\d+)
(?<unit>
(?: [s] (?:econd )? ) |
(?: (?:Second ) ) |
(?: [M] (?:inute )? ) |
(?: (?:minute ) ) |
(?: [h] (?:our )? ) |
(?: (?:Hour ) ) |
(?: [Dd] (?:ay )? ) |
(?: [Ww] (?:eek )? ) |
(?: [m] (?:onth )? ) |
(?: (?:Month ) ) |
(?: [Yy] (?:ear )? )
)(?:(?<=[\D]{3})[s])?
(?<direction> [Aa]go)
$
"@
function $regEx([int]$amount, [string]$unit, [string]$direction)
{
if($direction.ToLower() -eq "ago")
{
$amount = $amount*-1;
}
else
{
throw "Unsupported direction: $direction";
}
[DateTime]$now = Get-Date;
[DateTime]$result;
switch -Regex ($unit)
{
"(?x-i) s | (?i) second" {$result = $now.AddSeconds($amount);}
"(?xi) M | (?i) minute" {$result = $now.AddMinutess($amount);}
"(?x-i) h | (?i) hour" {$result = $now.AddHours($amount);}
"(?xi) d | (?i) days" {$result = $now.AddDays($amount);}
"(?xi) w | (?i) week" {$result = $now.AddDays($amount*7);}
"(?x-i) m | (?i) month" {$result = $now.AddMonths($amount);}
"(?xi) y | (?i) year" {$result = $now.AddYears($amount);}
Default {throw "Unsupported unit: $unit"}
}
return $result;
}
Proposed technical implementation details (optional)
Perhaps something along this way...
- Give the function a unique internal name just to keep track of it
- As a last step, when no other function, cmdLet, application or whatever is found, then use the regEx on the potential command and see if it match.
- If it match...
- make a temporary copy of the function from step 1 (or assign it an alternative name or something)
- Name the temporary copy in such a way that it would have ben executed if it had existed as a "normal function"
- Call this new temporary function, with the named capture groups from the regex as it's input parameters
- cleanup and remove all temporary stuff
Metadata
Metadata
Assignees
Labels
Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifResolution-No ActivityIssue has had no activity for 6 months or moreIssue has had no activity for 6 months or moreWG-Languageparser, language semanticsparser, language semantics