@@ -47,7 +47,8 @@ public class InvokeFormatterCommand : PSCmdlet, IOutputWriter
4747
4848 [ Parameter ( Mandatory = false ) ]
4949 [ ValidateNotNull ]
50- public object Range { get ; set ; }
50+ [ ValidateCount ( 4 , 4 ) ]
51+ public int [ ] Range { get ; set ; }
5152
5253#if DEBUG
5354 /// <summary>
@@ -79,18 +80,7 @@ protected override void BeginProcessing()
7980 }
8081#endif
8182
82- try
83- {
84- SetRange ( ) ;
85- }
86- catch ( Exception e )
87- {
88- this . ThrowTerminatingError ( new ErrorRecord (
89- e ,
90- "RANGE_ERROR" ,
91- ErrorCategory . InvalidArgument ,
92- Range ) ) ;
93- }
83+ this . range = new Range ( Range [ 0 ] , Range [ 1 ] , Range [ 2 ] , Range [ 3 ] ) ;
9484
9585 try
9686 {
@@ -125,59 +115,6 @@ protected override void ProcessRecord()
125115 this . WriteObject ( formattedScriptDefinition ) ;
126116 }
127117
128- private void SetRange ( )
129- {
130- if ( Range == null )
131- {
132- this . range = null ;
133- return ;
134- }
135-
136- // When the range object is constructed with `[range]::new syntax`, `Range as Range` cast works.
137- // However, if the range object is constructed with `new-object "range"` syntax, then
138- // we need to use the ImmediatedBaseObject property to cast.
139- var range = ( Range as Range ?? ( Range as PSObject ) ? . ImmediateBaseObject as Range ) ;
140- if ( range != null )
141- {
142- this . range = range ;
143- return ;
144- }
145-
146- var objArr = Range as object [ ] ;
147- int [ ] intArr ;
148- if ( objArr != null )
149- {
150- if ( ! objArr . All ( x => x is int ) )
151- {
152- throw new ArgumentException (
153- "Array should contain integer elements." ,
154- nameof ( Range ) ) ;
155- }
156- intArr = new int [ objArr . Length ] ;
157- objArr . CopyTo ( intArr , 0 ) ;
158- }
159- else
160- {
161- // todo check passing int[] casted parameter
162- intArr = Range as int [ ] ;
163- if ( intArr == null )
164- {
165- throw new ArgumentException (
166- "Argument should be of type Range or object[] or int[]." ,
167- nameof ( Range ) ) ;
168- }
169- }
170-
171- if ( intArr . Length != 4 )
172- {
173- throw new ArgumentException (
174- "Array should be of length 4." ,
175- nameof ( Range ) ) ;
176- }
177-
178- this . range = new Range ( intArr [ 0 ] , intArr [ 1 ] , intArr [ 2 ] , intArr [ 3 ] ) ;
179- }
180-
181118 private void ValidateInputSettings ( )
182119 {
183120 // todo implement this
0 commit comments