@@ -11,6 +11,7 @@ import {
1111 getDefaultOptions ,
1212} from "../util/getDefaultArguments.js" ;
1313import { parseArgs } from "../util/parseArgs.js" ;
14+ import { printHelp } from "../util/printHelp.js" ;
1415
1516suite ( "CLI" , ( ) => {
1617 test ( "formats a file in place" , async ( ) => {
@@ -180,6 +181,8 @@ suite("CLI", () => {
180181 const cli : CLI = {
181182 binName : "talon-fmt" ,
182183 fileEndings : [ "txt" ] ,
184+ supportedFlagArgs : [ "--indent-tabs" ] ,
185+ supportedValueArgs : [ "--indent-width" , "--column-width" ] ,
183186 format : ( text , receivedOptions , receivedFileName ) => {
184187 actualText = text ;
185188 actualOptions = receivedOptions ;
@@ -211,6 +214,8 @@ suite("CLI", () => {
211214 const cli : CLI = {
212215 binName : "talon-fmt" ,
213216 fileEndings : [ "txt" ] ,
217+ supportedFlagArgs : [ "--indent-tabs" ] ,
218+ supportedValueArgs : [ "--indent-width" , "--column-width" ] ,
214219 format : ( text , receivedOptions , receivedFileName ) => {
215220 actualText = text ;
216221 actualOptions = receivedOptions ;
@@ -232,7 +237,10 @@ suite("CLI", () => {
232237 filePatterns : [ "a.txt" , "b.txt" ] ,
233238 check : true ,
234239 } ) ;
235- const actual = parseArgs ( [ "--check" , "a.txt" , "b.txt" ] ) ;
240+ const actual = parseArgs (
241+ createCLI ( ( ) => "" ) ,
242+ [ "--check" , "a.txt" , "b.txt" ] ,
243+ ) ;
236244
237245 assert . deepEqual ( actual , expected ) ;
238246 } ) ;
@@ -242,7 +250,10 @@ suite("CLI", () => {
242250 filePatterns : [ "--check" ] ,
243251 check : true ,
244252 } ) ;
245- const actual = parseArgs ( [ "--check" , "--" , "--check" ] ) ;
253+ const actual = parseArgs (
254+ createCLI ( ( ) => "" ) ,
255+ [ "--check" , "--" , "--check" ] ,
256+ ) ;
246257
247258 assert . deepEqual ( actual , expected ) ;
248259 } ) ;
@@ -258,38 +269,132 @@ suite("CLI", () => {
258269 lineWidth : 80 ,
259270 columnWidth : 24 ,
260271 } ) ;
261- const actual = parseArgs ( [
262- "--indent-tabs" ,
263- "--indent-width" ,
264- "2" ,
265- "--line-width" ,
266- "80" ,
267- "--column-width" ,
268- "24" ,
269- "a.txt" ,
270- ] ) ;
272+ const actual = parseArgs (
273+ createCLI ( ( ) => "" ) ,
274+ [
275+ "--indent-tabs" ,
276+ "--indent-width" ,
277+ "2" ,
278+ "--column-width" ,
279+ "24" ,
280+ "a.txt" ,
281+ ] ,
282+ ) ;
283+
284+ assert . deepEqual ( actual , expected ) ;
285+ } ) ;
286+
287+ test ( "rejects unsupported formatter arguments" , ( ) => {
288+ const snippetCli : CLI = {
289+ ...createCLI ( ( ) => "" ) ,
290+ binName : "snippet-fmt" ,
291+ supportedFlagArgs : [ ] ,
292+ supportedValueArgs : [ ] ,
293+ } ;
294+
295+ assert . throws (
296+ ( ) => parseArgs ( snippetCli , [ "--indent-width" , "2" ] ) ,
297+ / U n k n o w n a r g u m e n t : - - i n d e n t - w i d t h / ,
298+ ) ;
299+ } ) ;
300+
301+ test ( "rejects unsupported formatter flags" , ( ) => {
302+ const snippetCli : CLI = {
303+ ...createCLI ( ( ) => "" ) ,
304+ binName : "snippet-fmt" ,
305+ supportedFlagArgs : [ ] ,
306+ } ;
307+
308+ assert . throws (
309+ ( ) => parseArgs ( snippetCli , [ "--indent-tabs" ] ) ,
310+ / U n k n o w n a r g u m e n t : - - i n d e n t - t a b s / ,
311+ ) ;
312+ } ) ;
313+
314+ test ( "parses only supported arguments for current cli" , ( ) => {
315+ const expected = getArguments ( {
316+ filePatterns : [ "a.txt" ] ,
317+ indentTabs : true ,
318+ indentWidth : 2 ,
319+ columnWidth : 24 ,
320+ } ) ;
321+ const actual = parseArgs (
322+ createCLI ( ( ) => "" ) ,
323+ [
324+ "--indent-tabs" ,
325+ "--indent-width" ,
326+ "2" ,
327+ "--column-width" ,
328+ "24" ,
329+ "a.txt" ,
330+ ] ,
331+ ) ;
271332
272333 assert . deepEqual ( actual , expected ) ;
273334 } ) ;
274335
336+ test ( "prints help only for supported arguments" , async ( ) => {
337+ const cli : CLI = {
338+ binName : "tree-sitter-fmt" ,
339+ fileEndings : [ "scm" ] ,
340+ supportedFlagArgs : [ "--indent-tabs" ] ,
341+ supportedValueArgs : [ "--indent-width" ] ,
342+ format : ( text ) => Promise . resolve ( text ) ,
343+ } ;
344+
345+ const output = await captureStreamWrite ( process . stdout , ( ) => {
346+ printHelp ( cli ) ;
347+ return Promise . resolve ( ) ;
348+ } ) ;
349+
350+ assert . equal (
351+ output . text ,
352+ [
353+ "Usage: tree-sitter-fmt [options] [file/dir/glob ...]" ,
354+ "" ,
355+ "Flags:" ,
356+ " --help" ,
357+ " --version" ,
358+ " --check" ,
359+ " --indent-tabs" ,
360+ "" ,
361+ "Options:" ,
362+ " --indent-width <n>" ,
363+ "" ,
364+ ] . join ( "\n" ) ,
365+ ) ;
366+ } ) ;
367+
275368 test ( "rejects unknown arguments" , ( ) => {
276369 assert . throws (
277- ( ) => parseArgs ( [ "--check" , "--write" ] ) ,
370+ ( ) =>
371+ parseArgs (
372+ createCLI ( ( ) => "" ) ,
373+ [ "--check" , "--write" ] ,
374+ ) ,
278375 / U n k n o w n a r g u m e n t : - - w r i t e / ,
279376 ) ;
280377 } ) ;
281378
282379 test ( "rejects missing width values" , ( ) => {
283380 assert . throws (
284- ( ) => parseArgs ( [ "--indent-width" ] ) ,
381+ ( ) =>
382+ parseArgs (
383+ createCLI ( ( ) => "" ) ,
384+ [ "--indent-width" ] ,
385+ ) ,
285386 / M i s s i n g v a l u e f o r a r g u m e n t : - - i n d e n t - w i d t h / ,
286387 ) ;
287388 } ) ;
288389
289390 test ( "rejects invalid width values" , ( ) => {
290391 assert . throws (
291- ( ) => parseArgs ( [ "--line-width" , "0" ] ) ,
292- / I n v a l i d v a l u e f o r - - l i n e - w i d t h : 0 / ,
392+ ( ) =>
393+ parseArgs (
394+ createCLI ( ( ) => "" ) ,
395+ [ "--indent-width" , "0" ] ,
396+ ) ,
397+ / I n v a l i d v a l u e f o r - - i n d e n t - w i d t h : 0 / ,
293398 ) ;
294399 } ) ;
295400} ) ;
@@ -313,6 +418,8 @@ function createCLI(format: (text: string) => string | Promise<string>): CLI {
313418 return {
314419 binName : "talon-fmt" as const ,
315420 fileEndings : [ "txt" ] ,
421+ supportedFlagArgs : [ "--indent-tabs" ] ,
422+ supportedValueArgs : [ "--indent-width" , "--column-width" ] ,
316423 format : ( text : string ) => Promise . resolve ( format ( text ) ) ,
317424 } ;
318425}
0 commit comments