@@ -24,6 +24,7 @@ namespace chocolatey.infrastructure.app.builders
24
24
using System . Text ;
25
25
using adapters ;
26
26
using attributes ;
27
+ using chocolatey . infrastructure . app . commands ;
27
28
using configuration ;
28
29
using cryptography ;
29
30
using extractors ;
@@ -392,175 +393,7 @@ private static void set_global_options(IList<string> args, ChocolateyConfigurati
392
393
( ) => { } ,
393
394
( ) =>
394
395
{
395
- var commandsLog = new StringBuilder ( ) ;
396
- IEnumerable < ICommand > commands = container . GetAllInstances < ICommand > ( ) ;
397
- foreach ( var command in commands . or_empty_list_if_null ( ) )
398
- {
399
- var attributes = command . GetType ( ) . GetCustomAttributes ( typeof ( CommandForAttribute ) , false ) . Cast < CommandForAttribute > ( ) ;
400
- foreach ( var attribute in attributes . or_empty_list_if_null ( ) )
401
- {
402
- commandsLog . AppendFormat ( " * {0} - {1}\n " , attribute . CommandName , attribute . Description ) ;
403
- }
404
- }
405
-
406
- "chocolatey" . Log ( ) . Info ( @"This is a listing of all of the different things you can pass to choco.
407
- " ) ;
408
- "chocolatey" . Log ( ) . Info ( ChocolateyLoggers . Important , "Commands" ) ;
409
- "chocolatey" . Log ( ) . Info ( @"
410
- {0}
411
-
412
- Please run chocolatey with `choco command -help` for specific help on
413
- each command.
414
- " . format_with ( commandsLog . ToString ( ) ) ) ;
415
- "chocolatey" . Log ( ) . Info ( ChocolateyLoggers . Important , @"How To Pass Options / Switches" ) ;
416
- "chocolatey" . Log ( ) . Info ( @"
417
- You can pass options and switches in the following ways:
418
-
419
- * Unless stated otherwise, an option/switch should only be passed one
420
- time. Otherwise you may find weird/non-supported behavior.
421
- * `-`, `/`, or `--` (one character switches should not use `--`)
422
- * **Option Bundling / Bundled Options**: One character switches can be
423
- bundled. e.g. `-d` (debug), `-f` (force), `-v` (verbose), and `-y`
424
- (confirm yes) can be bundled as `-dfvy`.
425
- * NOTE: If `debug` or `verbose` are bundled with local options
426
- (not the global ones above), some logging may not show up until after
427
- the local options are parsed.
428
- * **Use Equals**: You can also include or not include an equals sign
429
- `=` between options and values.
430
- * **Quote Values**: When you need to quote an entire argument, such as
431
- when using spaces, please use a combination of double quotes and
432
- apostrophes (`""'value'""`). In cmd.exe you can just use double quotes
433
- (`""value""`) but in powershell.exe you should use backticks
434
- (`` `""value`"" ``) or apostrophes (`'value'`). Using the combination
435
- allows for both shells to work without issue, except for when the next
436
- section applies.
437
- * **Pass quotes in arguments**: When you need to pass quoted values to
438
- to something like a native installer, you are in for a world of fun. In
439
- cmd.exe you must pass it like this: `-ia ""/yo=""""Spaces spaces""""""`. In
440
- PowerShell.exe, you must pass it like this: `-ia '/yo=""""Spaces spaces""""'`.
441
- No other combination will work. In PowerShell.exe if you are on version
442
- v3+, you can try `--%` before `-ia` to just pass the args through as is,
443
- which means it should not require any special workarounds.
444
- * **Periods in PowerShell**: If you need to pass a period as part of a
445
- value or a path, PowerShell doesn't always handle it well. Please
446
- quote those values using ""Quote Values"" section above.
447
- * Options and switches apply to all items passed, so if you are
448
- installing multiple packages, and you use `--version=1.0.0`, choco
449
- is going to look for and try to install version 1.0.0 of every
450
- package passed. So please split out multiple package calls when
451
- wanting to pass specific options.
452
- " ) ;
453
- "chocolatey" . Log ( ) . Info ( ChocolateyLoggers . Important , @"Scripting / Integration - Best Practices / Style Guide" ) ;
454
- "chocolatey" . Log ( ) . Info ( @"
455
- When writing scripts, such as PowerShell scripts passing options and
456
- switches, there are some best practices to follow to ensure that you
457
- don't run into issues later. This also applies to integrations that
458
- are calling Chocolatey and parsing output. Chocolatey **uses**
459
- PowerShell, but it is an exe, so it cannot return PowerShell objects.
460
-
461
- Following these practices ensures both readability of your scripts AND
462
- compatibility across different versions and editions of Chocolatey.
463
- Following this guide will ensure your experience is not frustrating
464
- based on choco not receiving things you think you are passing to it.
465
-
466
- * For consistency, always use `choco`, not `choco.exe`. Never use
467
- shortcut commands like `cinst` or `cup`.
468
- * Always have the command as the first argument to `choco`. e.g.
469
- `choco install`, where `install` is the command.
470
- * If there is a subcommand, ensure that is the second argument. e.g.
471
- `choco source list`, where `source` is the command and `list` is the
472
- subcommand.
473
- * Typically the subject comes next. If installing packages, the
474
- subject would be the package names, e.g. `choco install pkg1 pkg2`.
475
- * Never use 'nupkg' or point directly to a nupkg file UNLESS using
476
- 'choco push'. Use the source folder instead, e.g. `choco install
477
- <package id> --source=""'c:\folder\with\package'""` instead of
478
- `choco install DoNotDoThis.1.0.nupkg` or `choco install DoNotDoThis
479
- --source=""'c:\folder\with\package\DoNotDoThis.1.0.nupkg'""`.
480
- * Switches and parameters are called simply options. Options come
481
- after the subject. e.g. `choco install pkg1 --debug --verbose`.
482
- * Never use the force option (`--force`/`-f`) in scripts (or really
483
- otherwise as a default mode of use). Force is an override on
484
- Chocolatey behavior. If you are wondering why Chocolatey isn't doing
485
- something like the documentation says it should, it's likely because
486
- you are using force. Stop.
487
- * Always use full option name. If the short option is `-n`, and the
488
- full option is `--name`, use `--name`. The only acceptable short
489
- option for use in scripts is `-y`. Find option names in help docs
490
- online or through `choco -?` /`choco [Command Name] -?`.
491
- * For scripts that are running automated, always use `-y`. Do note
492
- that even with `-y` passed, some things / state issues detected will
493
- temporarily stop for input - the key here is temporarily. They will
494
- continue without requiring any action after the temporary timeout
495
- (typically 30 seconds).
496
- * Full option names are prepended with two dashes, e.g. `--` or
497
- `--debug --verbose --ignore-proxy`.
498
- * When setting a value to an option, always put an equals (`=`)
499
- between the name and the setting, e.g. `--source=""'local'""`.
500
- * When setting a value to an option, always surround the value
501
- properly with double quotes bookending apostrophes, e.g.
502
- `--source=""'internal_server'""`.
503
- * If you are building PowerShell scripts, you can most likely just
504
- simply use apostrophes surrounding option values, e.g.
505
- `--source='internal_server'`.
506
- * Prefer upgrade to install in scripts. You can't `install` to a newer
507
- version of something, but you can `choco upgrade` which will do both
508
- upgrade or install (unless switched off explicitly).
509
- * If you are sharing the script with others, pass `--source` to be
510
- explicit about where the package is coming from. Use full link and
511
- not source name ('https://chocolatey.org/api/v2' versus
512
- 'chocolatey').
513
- * If parsing output, you might want to use `--limit-output`/`-r` to
514
- get output in a more machine parseable format. NOTE: Not all
515
- commands handle return of information in an easily digestible
516
- output.
517
- * Use exit codes to determine status. Chocolatey exits with 0 when
518
- everything worked appropriately and other exits codes like 1 when
519
- things error. There are package specific exit codes that are
520
- recommended to be used and reboot indicating exit codes as well. To
521
- check exit code when using PowerShell, immediately call
522
- `$exitCode = $LASTEXITCODE` to get the value choco exited with.
523
-
524
- Here's an example following bad practices (line breaks added for
525
- readability):
526
-
527
- `choco install pkg1 -y -params '/Option:Value /Option2:value with
528
- spaces' --c4b-option 'Yaass' --option-that-is-new 'dude upgrade'`
529
-
530
- Now here is that example written with best practices (again line
531
- breaks added for readability - there are not line continuations
532
- for choco):
533
-
534
- `choco upgrade pkg1 -y --source=""'https://chocolatey.org/api/v2'""
535
- --package-parameters=""'/Option:Value /Option2:value with spaces'""
536
- --c4b-option=""'Yaass'"" --option-that-is-new=""'dude upgrade'""`
537
-
538
- Note the differences between the two:
539
- * Which is more self-documenting?
540
- * Which will allow for the newest version of something installed or
541
- upgraded to (which allows for more environmental consistency on
542
- packages and versions)?
543
- * Which may throw an error on a badly passed option?
544
- * Which will throw errors on unknown option values? See explanation
545
- below.
546
-
547
- Chocolatey ignores options it doesn't understand, but it can only
548
- ignore option values if they are tied to the option with an
549
- equals sign ('='). Note those last two options in the examples above?
550
- If you roll off of a commercial edition or someone with older version
551
- attempts to run the badly crafted script `--c4b-option 'Yaass'
552
- --option-that-is-new 'dude upgrade'`, they are likely to see errors on
553
- 'Yaass' and 'dude upgrade' because they are not explicitly tied to the
554
- option they are written after. Now compare that to the other script.
555
- Choco will ignore `--c4b-option=""'Yaass'""` and
556
- `--option-that-is-new=""'dude upgrade'""` as a whole when it doesn't
557
- register the options. This means that your script doesn't error.
558
-
559
- Following these scripting best practices will ensure your scripts work
560
- everywhere they are used and with newer versions of Chocolatey.
561
-
562
- " ) ;
563
- "chocolatey" . Log ( ) . Info ( ChocolateyLoggers . Important , "Default Options and Switches" ) ;
396
+ ChocolateyHelpCommand . display_help_message ( container ) ;
564
397
} ) ;
565
398
}
566
399
0 commit comments